1 / 22

คำสั่งแบบมีเงื่อนไข Conditional Statements

คำสั่งแบบมีเงื่อนไข Conditional Statements. C# Programming. เนื้อหา. นิพจน์ตรรกศาสตร์ คำสั่งแบบมีเงื่อนไข ตัวอย่าง โฟล์วชาร์ต. นิพจน์ตรรกศาสตร์. ตัวดำเนินการตรรกศาสตร์. ตัวอย่าง: นิพจน์ตรรกศาสตร์. double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________

Download Presentation

คำสั่งแบบมีเงื่อนไข Conditional 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. คำสั่งแบบมีเงื่อนไขConditional Statements C# Programming

  2. เนื้อหา • นิพจน์ตรรกศาสตร์ • คำสั่งแบบมีเงื่อนไข • ตัวอย่าง • โฟล์วชาร์ต

  3. นิพจน์ตรรกศาสตร์ • ตัวดำเนินการตรรกศาสตร์

  4. ตัวอย่าง: นิพจน์ตรรกศาสตร์ double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ true false true false true

  5. ตัวอย่าง double n1 = 78.0; double n2 = 80.0; n1 < n2 __________ n1 >= n2 __________ (n1 + 35) > n2 __________ Math.Abs(n1-n2) <= 0.001 __________ n1 == n2 __________ n1 != n2 __________ true false true false false true

  6. การผสมนิพจน์ตรรกศาสตร์ • ตัวดำเนินการตรรกศาสตร์ • && - และ (AND) • || - หรือ (OR) • !– ไม่ (NOT) • ตัวอย่าง • (x > 12) && (x < 20) • เป็นจริงถ้า x มีค่าอยู่ระหว่าง 12 และ 20 • ถ้า x เท่ากับ 10 แล้วนิพจน์ตรรกศาสตร์ข้างต้นเป็นเท็จ • ถ้า x เท่ากับ 15 แล้วนิพจน์ตรรกศาสตร์ข้างต้นเป็นจริง

  7. ลำดับการประมวลผล (Precedence Rules) • ( ) วงเล็บ • *, / , % • + – • <, >, <=, >= • ==, != • ! • && • ││ • ถ้าลำดับเท่ากันให้ประมวลผลจากซ้ายไปขวา

  8. คำสั่งแบบมีเงื่อนไข • if...else • switch...case

  9. C# Syntax Flowchart if (condition) statement; START condition true false Statement END คำสั่งif อนุญาตให้มีได้เพียงหนึ่งคำสั่งเท่านั้น

  10. Flowchart START condition true C# Syntax Statement if (condition) { statement1; statement2; : } false Statement END การผสมคำสั่ง • เราสามารถสร้างคำสั่งได้มากกว่าหนึ่งคำสั่งโดยการใส่คำสั่งไว้ใน {...}

  11. ตัวอย่าง: คำสั่ง if • อ่านตัวเลขหนึ่งจำนวน และหาว่าตัวเลขนั้นเป็นจำนวนคี่ หรือจำนวนคู่ n%2==0 false true using System; class EvenOrOdd { staticvoidMain() { int n = int.Parse(Console.ReadLine()); if (n % 2 == 0) Console.WriteLine("{0} is even", n); if (n % 2 == 1) Console.WriteLine("{0} is odd", n); } } Print "even" n%2==1 false true Print "odd" END

  12. Flowchart START true condition false Statementt Statementf C# Syntax if (condition) statementt; else statementf; END คำสั่งif…else…

  13. ตัวอย่าง: คำสั่ง if...else • อ่านตัวเลขหนึ่งจำนวน และหาว่าตัวเลขนั้นเป็นจำนวนคี่ หรือจำนวนคู่ using System; class EvenOrOdd { staticvoidMain() { int n = int.Parse(Console.ReadLine()); if (n % 2 == 0) Console.WriteLine("{0} is even", n); else Console.WriteLine("{0} is odd", n); } } n%2==0 true false even odd END

  14. if หลายชั้น condition1 true false condition2 condition3 true false false true stmt2a stmt2b stmt3a

  15. Read n n > 0 true false n < 0 true false Print "positive" Print "negative" Print "zero" END ตัวอย่าง: if หลายชั้น • อ่านตัวเลขหนึ่งจำนวน และหาว่าตัวเลขนั้นเป็นค่าบวก ค่าลบ หรือค่าศูนย์

  16. ตัวอย่าง: โปรแกรม C# using System; class Sign { staticvoidMain() { int n = int.Parse(Console.ReadLine()); if (n > 0) Console.WriteLine("{0} is positive", n); else { if (n < 0) Console.WriteLine("{0} is negative", n); else Console.WriteLine("{0} is zero", n); } } }

  17. true x==1 Action1; false true x==2 Action2; false true x==3 Action3; false true x==4 Action4; false Default_Action; การกำหนดหลายเงื่อนไข

  18. true x==1 Action1; false true x==2 Action2; false true x==3 Action3; false true x==4 Action4; false Default_Action; การกำหนดหลายเงื่อนไขกับคำสั่ง if if (x==1) Action1; else if (x==2) Action2; else if (x==3) Action3; else if (x==4) Action4; else Default_Action;

  19. true x==1 Action1; false true x==2 Action2; false true x==3 Action3; false true x==4 Action4; false Default_Action; การใช้คำสั่ง switch…case switch (x) { case 1: Action1; break; case 2: Action2; break; case 3: Action3; break; case 4: Action4; break; default: Default_Action; break; }

  20. ไวยากรณ์ switch…case • Syntax: • <expression> ต้องเป็นประเภทข้อมูล int, char หรือ string switch (<expression>) { case<constant-expression>: <statements>; break; default: <statements>; break; }

  21. Operator: LEMON Promotion Type Program Payment price Usage time แบบฝึกหัด • คำนวนค่าใช้จ่ายโทรศัพท์โดยคิดตามเวลาที่ใช้ อินพุท: ประเภทโปรโมชั่น (promotion type) จำนวนเวลาที่ใช้ (usage time) เอาท์พุท: ค่าใช้จ่าย (payment price)

  22. Condition Problems สรุป • นิพจน์ตรรกศาสตร์ • คำสั่งแบบมีเงื่อนไข • if...else... • switch-case if…else… switch

More Related