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
Control Statements
เอกสารประกอบการบรรยายรายวิ�ชา204200 Browser-Based Application Development
วิ�ตถุ�ประสงค์�นั�กศึ�กษาสามารถุอธิ�บายเก��ยวิก�บ Control Statements ที่��ส�าค์�ญในั C# ได้$
นั�กศึ�กษาสามารถุพั�ฒนัาโปรแกรมประย�กต�บนับราวิเซอร�อย*างง*ายๆ โด้ยใช$ Control Statements ในัการแก$ป,ญหาและค์วิบค์�มต*างๆ
ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch
ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch
Control Statements
Structural ProgrammingSequenceDecisionLooping
Control Statements
Statementsโด้ยปกต�แล$วิโปรแกรมจะที่�างานั
ตามล�าด้�บค์�าส��งโปรแกรมจะม�ประโยชนั�มากข้�1นัถุ$า
เราสามารถุค์วิบค์�มล�าด้�บข้องการที่�างานัข้องค์�าส��ง
Control Statements
Statements การก�าหนัด้ค์*า (Assignment)อ�นัพั�ต/เอาที่�พั�ต (Input/Output)
ค์วิบค์�ม (Control)
Control Statements
โปรแกรมจะต�ด้ส�นัใจวิ*าจะที่�าอะไรเป2นัล�าด้�บต*อไป
Control Statements
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไป
ที่�าซ�1าส*วินัข้องโปรแกรม
Control Statements
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิต�วิเล3อก
Control Statements
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ
เที่*าก�นั ไม*เที่*าก�นัมากกวิ*า นั$อยกวิ*า
Control Statements
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ
เที่*าก�นั ไม*เที่*าก�นัมากกวิ*า นั$อยกวิ*า
Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิ“ถุ$าพัรรค์ไที่ยร�กใค์รได้$ค์ะแนันัมากกวิ*า
พัรรค์ประชาธิ�ปไตยแล$วินัายที่�กที่ายจะได้$เป2นันัายกม�ฉะนั�1นันัายเช�ญจะได้$เป2นันัายก”
Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้
ต*อไปถุ$า แล$วิ
ถุ$า ไที่ยร�กใค์ร > ประชาธิ�ปไตยนัายที่�กที่าย = นัายก
ม�ฉะนั�1นันัายเช�ญ = นัายก
Control Statements เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้
ต*อไปถุ$า แล$วิif ไที่ยร�กใค์ร > ประชาธิ�ปไตย
นัายที่�กที่าย = นัายกelse
นัายเช�ญ = นัายก
If Else
if ( <expression> ) <statement1>;
else ( <expression> ) <statement2>;
If Else
if (count < 50) count++;
If Elseif (RadioButtonListSex.SelectedItem.Value == "M")
LabelSex.Text = "ชาย";else
LabelSex.Text = "หญ�ง";if (CheckBoxNews.Checked == true)
LabelNews.Text = "ร�บข้*าวิสาร";else
LabelNews.Text = "ไม*ร�บข้*าวิสาร";
If Else
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปถุ$า แล$วิต�วิเล3อก
If Else
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไปต�วิเล3อก
1, 2, 3 หร3อ 4
Switch Case
switch ( <expression> ) <statement>
case <constant expression> :
default :
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 */
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); }
Switch Caseswitch
(RadioButtonListSex.SelectedIndex) { case ‘1’: Label1.Text = “Male”;
break; case ‘2’: Label1.Text =
“Female”; break; default: exit(1);}
ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch
Control Statements
เล3อกวิ*าจะที่�าส*วินัข้องโปรแกรมใด้ต*อไป
ที่�าซ�1าส*วินัข้องโปรแกรม
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)For
For Loops
Syntax: for ( [<expr1>] ; [<expr2>] ; [<expr3>] ) <statement>
For Loops
for (count = 1; count <= 10; count++){ Response.Write(coutn + “<BR>",);}
For Loops
for (i=0; i<100; i++)
sum += i;
For Loops
for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20) Response.Write(fahr + " " + (5.0/9.0)*(fahr-32) + "<BR>");
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
For Loops
for (i=0; i<100; i++)
sum += x[i];
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)While/Do While
While Loops
Syntax: while ( <expression> ) <statement>
While Loops int count = 1;
while (count <= 100) { Response.Write(count + "<BR>"); count += 1; /* Shorthand for count = count + 1 */ }
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)While/Do While
Do While Loops
Syntax: do <statement> while ( <expression> );
Do While Loops
i = 1; n = 1; do { n *= i; i++; } while (i <= factorial);
Do While Loops
int i = 0;Do{ Response.Write(i +
"<BR>");i++;
} while (i<100)
Control Statements
ที่�าซ�1าส*วินัข้องโปรแกรม (Loops)ForWhile/Do While
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);
ห�วิข้$อบรรยาย Control Statements if, else do, while for foreach switch, case try, catch