1 / 45

Control Statements

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

reba
Download Presentation

Control Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Statements เอกสารประกอบการบรรยายรายวิชา204200Browser-Based Application Development

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

  3. หัวข้อบรรยาย • Control Statements • if, else • do, while • for • foreach • switch, case • try, catch

  4. หัวข้อบรรยาย • Control Statements • if, else • do, while • for • foreach • switch, case • try, catch

  5. Control Statements • Structural Programming • Sequence • Decision • Looping

  6. Control Statements • Statements • โดยปกติแล้วโปรแกรมจะทำงานตามลำดับคำสั่ง • โปรแกรมจะมีประโยชน์มากขึ้นถ้าเราสามารถควบคุมลำดับของการทำงานของคำสั่ง

  7. Control Statements • Statements • การกำหนดค่า (Assignment) • อินพุต/เอาท์พุต (Input/Output) • ควบคุม (Control)

  8. Control Statements • โปรแกรมจะตัดสินใจว่าจะทำอะไรเป็นลำดับต่อไป

  9. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ทำซ้ำส่วนของโปรแกรม

  10. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว • ตัวเลือก

  11. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว • เท่ากัน ไม่เท่ากัน • มากกว่า น้อยกว่า

  12. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว • เท่ากัน ไม่เท่ากัน • มากกว่า น้อยกว่า

  13. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว “ถ้าพรรคไทยรักใครได้คะแนนมากกว่าพรรคประชาธิปไตยแล้วนายทักทายจะได้เป็นนายกมิฉะนั้นนายเชิญจะได้เป็นนายก”

  14. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว ถ้า ไทยรักใคร > ประชาธิปไตย นายทักทาย = นายก มิฉะนั้น นายเชิญ = นายก

  15. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว if ไทยรักใคร > ประชาธิปไตย นายทักทาย = นายก else นายเชิญ = นายก

  16. If Else if ( <expression> ) <statement1>; else ( <expression> ) <statement2>;

  17. If Else if (count < 50) count++;

  18. If Else if (RadioButtonListSex.SelectedItem.Value == "M") LabelSex.Text = "ชาย"; else LabelSex.Text = "หญิง"; if (CheckBoxNews.Checked == true) LabelNews.Text = "รับข่าวสาร"; else LabelNews.Text = "ไม่รับข่าวสาร";

  19. If Else • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ถ้า แล้ว • ตัวเลือก

  20. If Else • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ตัวเลือก • 1, 2, 3 หรือ 4

  21. Switch Case switch ( <expression> ) <statement> case <constant expression> : default :

  22. Switch Case switch (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 */

  23. Switch Case switch (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); }

  24. Switch Case switch (RadioButtonListSex.SelectedIndex) { case ‘1’: Label1.Text = “Male”; break; case ‘2’: Label1.Text = “Female”; break; default: exit(1); }

  25. หัวข้อบรรยาย • Control Statements • if, else • do, while • for • foreach • switch, case • try, catch

  26. Control Statements • เลือกว่าจะทำส่วนของโปรแกรมใดต่อไป • ทำซ้ำส่วนของโปรแกรม

  27. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • For • While/Do While

  28. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • For

  29. For Loops Syntax: for ( [<expr1>] ; [<expr2>] ; [<expr3>] ) <statement>

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

  31. For Loops for (i=0; i<100; i++) sum += i;

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

  33. 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

  34. For Loops for (i=0; i<100; i++) sum += x[i];

  35. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • For • While/Do While

  36. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • While/Do While

  37. While Loops Syntax: while ( <expression> ) <statement>

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

  39. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • While/Do While

  40. Do While Loops Syntax: do <statement> while ( <expression> );

  41. Do While Loops i = 1; n = 1; do { n *= i; i++; } while (i <= factorial);

  42. Do While Loops int i = 0; Do { Response.Write(i + "<BR>"); i++; } while (i<100)

  43. Control Statements • ทำซ้ำส่วนของโปรแกรม (Loops) • For • While/Do While

  44. Control Statements     int i = 10;    do {        Response.Write(i);        i = i - 1;    } while (i > 0);     int i;    for (i = 10; i > 0; i--)    {        Response.Write(i);     }    int i;    for (i = 10; i > 0; i--)    {        Response.Write(i);    }

  45. หัวข้อบรรยาย • Control Statements • if, else • do, while • for • foreach • switch, case • try, catch

More Related