1 / 24

SCCS031 Principle of Computer Programming

0 4. Decision Algorithm. SCCS031 Principle of Computer Programming. Thinaphan Nithiyuwith Program of Computer Science & Information Technology http :// computer . pcru . ac . th / suchada/. Download & Contact. Download Documents http://computer.pcru.ac.th/suchada/SCCS031_Algo.php FAQ

meagan
Download Presentation

SCCS031 Principle of Computer Programming

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. 04 Decision Algorithm SCCS031 Principle of Computer Programming Thinaphan Nithiyuwith Program of Computer Science & Information Technology http://computer.pcru.ac.th/suchada/

  2. Download & Contact • Download Documents • http://computer.pcru.ac.th/suchada/SCCS031_Algo.php • FAQ • http://computer.pcru.ac.th/suchada/com_title/show_forum.php • Contact ME • Office : Internet LABFloor 1 LC Building • Website : http://computer.pcru.ac.th/suchada/ • Phone : 056 - 717100 ต่อ 4503 • E-Mail : suchada@pcru.ac.th

  3. อัลกอริทึมแบบเงื่อนไขอัลกอริทึมแบบเงื่อนไข • การทำงานจะมีลักษณะเงื่อนไขให้ตัดสินใจ โดยเป็นทางเลือก สองทาง • คำสั่งแบบมีเงื่อนไข แบ่งออกเป็น 2 คำสั่ง 1. IFStatement แบ่งเป็น 3 ลักษณะ - IF…Then… - IF…Then….Else… - IF…Then…Else…ซ้อนกันหลายชั้น 2. Select Statement

  4. คำสั่ง IF…Then • ใช้ในการเขียนโปรแกรมที่สั่งให้เครื่องคอมพิวเตอร์ตัดสินใจเลือกเงื่อนไขที่เป็นจริง แล้วให้ปฏิบัติตามคำสั่งที่ต้องการ โดยมีทางเลือก 1 ทางเท่านั้น • รูปแบบ If เงื่อนไขThen คำสั่ง 1

  5. Start x y x = 0 n Hello Bye End ตัวอย่างการใช้คำสั่ง If..Then • หากใส่ค่า x เป็น 0 แล้วกดปุ่มทดสอบ จะแสดงข้อความว่า Hello และ Bye • หากใส่ค่า อื่น จะแสดงคำว่า Bye

  6. ตัวอย่างการใช้คำสั่ง If..Then • ให้สร้างหน้าฟอร์ม ดังนี้ Code ปุ่มทดสอบ Private Sub Command1_Click() If Text1.Text = 0 Then Text2.Text = "Hello" Text3.Text = " Bye“ End Sub Code ปุ่มClear Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub • หากใส่ค่า x เป็น 0 แล้วกดปุ่มทดสอบ จะแสดงข้อความว่า Hello และ Bye • หากใส่ค่า อื่น จะแสดงคำว่า Bye

  7. คำสั่ง If…Then…Else Statement • เป็นคำสั่งที่มีการตรวจสอบเงื่อนไขแล้วมีทางเลือก 2 ทางเลือก โดยการพิจารณาจากเงื่อนไขที่ได้ • รูปแบบ If เงื่อนไขThen คำสั่ง 1 Else คำสั่ง 2 End If

  8. False True condition Statement ; Statement ; Flow-Chart : If…Then…Else Statement

  9. ตัวอย่างการใช้คำสั่ง If..Then..Else • หากใส่ค่า x เป็น 0 แล้วกดปุ่มทดสอบ จะแสดงข้อความว่า Equal zero และ Bye • หากใส่ค่า อื่น จะแสดงคำว่า Not Equal Zero และ Bye Start x y x = 0 n Not Equal zero Equal zero Bye End

  10. ตัวอย่างการใช้คำสั่ง If..Then..Else Code ปุ่มทดสอบ • ให้สร้างหน้าฟอร์ม ดังนี้ Private Sub Command1_Click() If Text1.Text = 0 Then Text2.Text = "Equal zero" Else Text2.Text = "Not Equal zero" End If Text3.Text = " Bye" End Sub Text1.Text Text2.Text Text3.Text Code ปุ่มClear Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub

  11. คำสั่ง IF…Then…Else…ซ้อนกันหลายชั้น • คำสั่ง IF ซ้อนกันจะเกิดขึ้นเมื่อทางเลือกนั้นมีมากกว่า 2 ทางเลือก If เงื่อนไข Then คำสั่ง 1 Elseif เงื่อนไขThen คำสั่ง 2 Elseif เงื่อนไขThen คำสั่ง 3 Else คำสั่ง 4 (คำสั่งที่ไม่ตรงเงื่อนไขใดเลย) End if

  12. Flow-Chart : IF…Then…Else…ซ้อนกันหลายชั้น

  13. เปรียบเทียบระหว่าง 2 ทางเลือก และ มากกว่า 2 ทางเลือก If เงื่อนไข Then คำสั่ง 1 Elseif เงื่อนไขThen คำสั่ง 2 Elseif เงื่อนไขThen คำสั่ง 3 Else คำสั่ง 4 End if If เงื่อนไขThen คำสั่ง 1 Else คำสั่ง 2 End If

  14. ตัวอย่างการใช้คำสั่ง If..Then..Else ซ้อนกันหลายชั้น Start • ป้อนตัวเลข 1 จำนวน เมื่อกดปุ่ม ทดสอบ • หากตัวเลขนั้นมากกว่า 0 ให้แสดงข้อความว่า More than Zero • หากตัวเลขนั้นน้อยกว่า 0 ให้แสดงข้อความว่า Less than Zero • หากตัวเลขนั้นมีค่าเป็น 0 ให้แสดงข้อความว่า Equal Zero • หลังจากนั้นให้แสดงคำว่า Bye x y x > 0 n y n More than Zero x < 0 Less than Zero Equal zero Bye End

  15. ตัวอย่างการใช้คำสั่ง If..Then..Else หลายชั้น Code ปุ่มตรวจสอบค่า • ให้สร้างหน้าฟอร์ม ดังนี้ Private Sub Command1_Click() If Text1.Text > 0 Then Text2.Text = "More Than Zero" ElseIf Text1.Text < 0 Then Text2.Text = "Less Than Zero" Else Text2.Text = "Equal Zero" End If Text3.Text = "Bye" End Sub Text1.Text Text1.Text Text1.Text Code ปุ่มClear Code ปุ่มClose Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub Private Sub Command3_Click() End End Sub

  16. คำสั่ง SelectCase • เป็นคำสั่งที่เหมาะสำหรับการตรวจสอบเงื่อนไขและมีทางเลือกให้ทำมากกว่า 2 ทางเลือก นิยมใช้ในกรณีที่เงื่อนไขการตัดสินใจขึ้นอยู่กับตัวแปรเพียงตัวเดียว คำสั่ง Select Case มีลักษณะการทำงานคล้ายกับคำสั่ง If…Then…Else แบบซ้อนกันหลายชั้น คือ มีการตัดสินใจในการทำงานมากกว่า 2 ทางเลือก แต่จะมีโครสร้างซับซ้อนน้อยกว่าคำสั่งแบบ If…Then…Else แบบซ้อนกันหลายชั้น

  17. รูปแบบ คำสั่ง Select Case Select Case ชื่อตัวแปรที่ตรวจสอบเงื่อนไข Case เงื่อนไข 1 หรือ ค่าคงที่ 1 คำสั่ง 1 Case เงื่อนไข 2 หรือ ค่าคงที่ 2 คำสั่ง 2 Case เงื่อนไข 3 หรือ ค่าคงที่ 3 คำสั่ง 3 Case เงื่อนไข n หรือ ค่าคงที่ n คำสั่ง n Case Else คำสั่ง...( คำสั่งที่ไม่ตรงกับ case ใด ๆ เลย ) End Select

  18. condition … value1 value2 else value n S ; Statement ; Statement ; S ; Flow-Chart : Select Case

  19. ตัวอย่างการใช้คำสั่ง Select Case Start • ป้อนตัวเลข 1 จำนวน เมื่อกดปุ่ม ทดสอบ • หากตัวเลขนั้นมากกว่า 0 ให้แสดงข้อความว่า More than Zero • หากตัวเลขนั้นน้อยกว่า 0 ให้แสดงข้อความว่า Less than Zero • หากตัวเลขนั้นมีค่าเป็น 0 ให้แสดงข้อความว่า Equal Zero • หลังจากนั้นให้แสดงคำว่า Bye x x else > 0 < 0 More than Zero Equal zero Less than Zero Bye End

  20. ตัวอย่างการใช้คำสั่ง Select Case Code ปุ่มตรวจสอบค่า • ให้สร้างหน้าฟอร์ม ดังนี้ Private Sub Command1_Click() Select Case Text1.Text Case Is > 0 Text2.Text = "More Than Zero" Case Is < 0 Text2.Text = "Less Than Zero" Case Else Text2.Text = "Equal Zero" End Select Text3.Text = "Bye" End Sub Text1.Text Text2.Text Text3.Text Code ปุ่มClear Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub Code ปุ่มClose Private Sub Command3_Click() End End Sub

  21. การตรวจสอบเกรด Text1.Text Text2.Text

  22. ตัวอย่างงาน Private Sub Command1_Click() Dim score As Integer Dim grade As String score = Val(Text2.Text)

  23. Select Case score Case Is >= 80: grade = "A" Case Is >= 70: grade = "B" Case Is >= 60: grade = "C" Case Is >= 50: grade = "D" Case Else: grade = "F" End Select Label3.Caption = Text1.Text & " your grade is " & grade End Sub

  24. FAQs?

More Related