45
Control Statements เเเเเเเเเเเเเเเเเเเเเ เเเเเเเ 204200 Browser-Based Application Development

Control Statements

  • Upload
    reba

  • View
    16

  • Download
    1

Embed Size (px)

DESCRIPTION

Control Statements. เอกสารประกอบการบรรยายรายวิชา 204200 Browser-Based Application Development. วัตถุประสงค์. นักศึกษาสามารถอธิบายเกี่ยวกับ Control Statements ที่สำคัญใน C# ได้ นักศึกษาสามารถพัฒนาโปรแกรมประยุกต์บนบราวเซอร์อย่างง่ายๆ โดยใช้ Control Statements ในการแก้ปัญหาและควบคุมต่างๆ. - PowerPoint PPT Presentation

Citation preview

Page 1: Control Statements

Control Statements

เอกสารประกอบการบรรยายรายวิ�ชา204200 Browser-Based Application Development

Page 2: Control Statements

วิ�ตถุ�ประสงค์�นั�กศึ�กษาสามารถุอธิ�บายเก��ยวิก�บ Control Statements ที่��ส�าค์�ญในั C# ได้$

นั�กศึ�กษาสามารถุพั�ฒนัาโปรแกรมประย�กต�บนับราวิเซอร�อย*างง*ายๆ โด้ยใช$ Control Statements ในัการแก$ป,ญหาและค์วิบค์�มต*างๆ

Page 3: Control Statements

ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch

Page 4: Control Statements

ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch

Page 5: Control Statements

Control Statements

Structural ProgrammingSequenceDecisionLooping

Page 6: Control Statements

Control Statements

Statementsโด้ยปกต�แล$วิโปรแกรมจะที่�างานั

ตามล�าด้�บค์�าส��งโปรแกรมจะม�ประโยชนั�มากข้�1นัถุ$า

เราสามารถุค์วิบค์�มล�าด้�บข้องการที่�างานัข้องค์�าส��ง

Page 7: Control Statements

Control Statements

Statements การก�าหนัด้ค์*า (Assignment)อ�นัพั�ต/เอาที่�พั�ต (Input/Output)

ค์วิบค์�ม (Control)

Page 8: Control Statements

Control Statements

โปรแกรมจะต�ด้ส�นัใจวิ*าจะที่�าอะไรเป2นัล�าด้�บต*อไป

Page 9: Control Statements

Control Statements

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไป

ที่�าซ�1าส*วินัข้องโปรแกรม

Page 10: Control Statements

Control Statements

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิต�วิเล3อก

Page 11: Control Statements

Control Statements

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ

เที่*าก�นั ไม*เที่*าก�นัมากกวิ*า นั$อยกวิ*า

Page 12: Control Statements

Control Statements

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ

เที่*าก�นั ไม*เที่*าก�นัมากกวิ*า นั$อยกวิ*า

Page 13: Control Statements

Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ“ถุ$าพัรรค์ไที่ยร�กใค์รได้$ค์ะแนันัมากกวิ*า

พัรรค์ประชาธิ�ปไตยแล$วินัายที่�กที่ายจะได้$เป2นันัายกม�ฉะนั�1นันัายเช�ญจะได้$เป2นันัายก”

Page 14: Control Statements

Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้

ต*อไปถุ$า แล$วิ

ถุ$า ไที่ยร�กใค์ร > ประชาธิ�ปไตยนัายที่�กที่าย = นัายก

ม�ฉะนั�1นันัายเช�ญ = นัายก

Page 15: Control Statements

Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้

ต*อไปถุ$า แล$วิif ไที่ยร�กใค์ร > ประชาธิ�ปไตย

นัายที่�กที่าย = นัายกelse

นัายเช�ญ = นัายก

Page 16: Control Statements

If Else

if ( <expression> ) <statement1>;

else ( <expression> ) <statement2>;

Page 17: Control Statements

If Else

if (count < 50) count++;

Page 18: Control Statements

If Elseif (RadioButtonListSex.SelectedItem.Value == "M")

LabelSex.Text = "ชาย";else

LabelSex.Text = "หญ�ง";if (CheckBoxNews.Checked == true)

LabelNews.Text = "ร�บข้*าวิสาร";else

LabelNews.Text = "ไม*ร�บข้*าวิสาร";

Page 19: Control Statements

If Else

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิต�วิเล3อก

Page 20: Control Statements

If Else

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปต�วิเล3อก

1, 2, 3 หร3อ 4

Page 21: Control Statements

Switch Case

switch ( <expression> ) <statement>

case <constant expression> :

default :

Page 22: Control Statements

Switch Caseswitch (i) {

case 1: Response.Write("one"); break; case 2: Response.Write("two"); break; case 3: Response.Write("three"); break; case 4: Response.Write("four"); break; default: printf("unrecognized number");

} /* end of switch */

Page 23: Control Statements

Switch Caseswitch (grade) { case ‘A’: Label1.Text = “Very GoOd!”;

break; case ‘B’: Label1.Text = “gOoD”; break; case ‘C’: Label1.Text = “Normal”;

break; case ‘D’: Label1.Text = “Poor”; break; case ‘F’: Label1.Text = “Failed”; break;

default: Label1.Text = “Error!”; exit(1); }

Page 24: Control Statements

Switch Caseswitch

(RadioButtonListSex.SelectedIndex) { case ‘1’: Label1.Text = “Male”;

break; case ‘2’: Label1.Text =

“Female”; break; default: exit(1);}

Page 25: Control Statements

ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch

Page 26: Control Statements

Control Statements

เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไป

ที่�าซ�1าส*วินัข้องโปรแกรม

Page 27: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While

Page 28: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)For

Page 29: Control Statements

For Loops

Syntax: for ( [<expr1>] ; [<expr2>] ; [<expr3>] ) <statement>

Page 30: Control Statements

For Loops

for (count = 1; count <= 10; count++){    Response.Write(coutn + “<BR>",);}

Page 31: Control Statements

For Loops

for (i=0; i<100; i++)

sum += i;

Page 32: Control Statements

For Loops

for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20) Response.Write(fahr + " " + (5.0/9.0)*(fahr-32) + "<BR>");

Page 33: Control Statements

For Loops 0 -17.8 20 -6.7 40 4.4 60 15.6 80 26.7 100 37.8 120 48.9 140 60.0 160 71.1 180 82.2 200 93.3 220 104.4 240 115.6 260 126.7 280 137.8 300 148.9

Page 34: Control Statements

For Loops

for (i=0; i<100; i++)

sum += x[i];

Page 35: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While

Page 36: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)While/Do While

Page 37: Control Statements

While Loops

Syntax: while ( <expression> ) <statement>

Page 38: Control Statements

While Loops  int count = 1;

    while (count <= 100)    {        Response.Write(count + "<BR>");        count += 1; /* Shorthand for count = count + 1 */    }

Page 39: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)While/Do While

Page 40: Control Statements

Do While Loops

Syntax: do <statement> while ( <expression> );

Page 41: Control Statements

Do While Loops

i = 1; n = 1; do { n *= i; i++; } while (i <= factorial);

Page 42: Control Statements

Do While Loops

int i = 0;Do{ Response.Write(i +

"<BR>");i++;

} while (i<100)

Page 43: Control Statements

Control Statements

ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While

Page 44: Control Statements

Control Statements     int i;

    for (i = 10; i > 0; i--)    {        Response.Write(i);     }

   int i;

    for (i = 10; i > 0; i--)    {        Response.Write(i);    }

    int i = 10;

    do {        Response.Write(i);        i = i - 1;    } while (i > 0);

Page 45: Control Statements

ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch