24
LOGO SCCS031 Principle of Computer SCCS031 Principle of Computer Programming Programming Thinaphan Nithiyuwith Program of Computer Science & Information Technology http://computer.pcru.ac.th/suchada/ Decision Decision Algorithm Algorithm 1 0 0 4 4

SCCS031 Principle of Computer Programming

  • Upload
    meagan

  • View
    26

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: SCCS031 Principle of Computer Programming

LOGO

SCCS031 Principle of Computer SCCS031 Principle of Computer Programming Programming

Thinaphan NithiyuwithProgram of Computer Science & Information Technologyhttp://computer.pcru.ac.th/suchada/

Decision Decision AlgorithmAlgorithm

1

00 44

Page 2: SCCS031 Principle of Computer Programming

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 LAB Floor 1 LC Building Website : http://computer.pcru.ac.th/suchada/ Phone : 056 - 717100 ต่�อ 4503 E-Mail : [email protected]

2

Page 3: SCCS031 Principle of Computer Programming

อั�ลกอัริ�ทึ�มแบบเงื่��อันไขอั�ลกอัริ�ทึ�มแบบเงื่��อันไข การทำ�างานจะมี�ลั�กษณะเง��อนไขให้�ต่�ดสิ�นใจ

โดยเป็"นทำางเลั�อก สิองทำาง คำ�าสิ��งแบบมี�เง��อนไข แบ�งออกเป็"น 2 คำ�าสิ��ง

1. IF Statement แบ�งเป็"น 3 ลั�กษณะ- IF…Then…- IF…Then….Else…- IF… Then…Else…ซ้�อนก�นห้ลัายชั้�)น

2. Select Statement

3

Page 4: SCCS031 Principle of Computer Programming

คำ��สั่��งื่ IF…Then ใชั้�ในการเข�ยนโป็รแกรมีทำ��สิ� �งให้�เคำร��อง

คำอมีพิ�วเต่อร,ต่�ดสิ�นใจเลั�อกเง��อนไขทำ��เป็"นจร�ง แลั�วให้�ป็ฏิ�บ�ต่�ต่ามีคำ�าสิ��งทำ��ต่�องการ โดยมี�ทำางเลั�อก 1 ทำางเทำ�าน�)น

ร.ป็แบบ If เง��อนไข Then คำ�าสิ��ง 1

Page 5: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then ห้ากใสิ�คำ�า x เป็"น 0 แลั�วกดป็/0มีทำดสิอบ จะแสิดง

ข�อคำวามีว�า Hello แลัะ Bye ห้ากใสิ�คำ�า อ��น จะแสิดงคำ�าว�า Bye

Start

x

x = 0

End

Bye

Hello

y

n

Page 6: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then ให้�สิร�างห้น�าฟอร,มี ด�งน�)

Private Sub Command1_Click() If Text1.Text = 0 Then Text2.Text = "Hello" Text3.Text = " Bye“End Sub

Code ป็/0มีทำดสิอบ

Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub

Code ป็/0มีClear

•ห้ากใสิ�คำ�า x เป็"น 0 แลั�วกดป็/0มีทำดสิอบ จะแสิดงข�อคำวามีว�า Hello แลัะ Bye•ห้ากใสิ�คำ�า อ��น จะแสิดงคำ�าว�า Bye

Page 7: SCCS031 Principle of Computer Programming

คำ��สั่��งื่ If…Then…Else Statement

เป็ นคำ��สั่��งื่ทึ!�ม!ก�ริตัริวจสั่อับเงื่��อันไขแล�วม!ทึ�งื่เล�อัก 2 ทึ�งื่เล�อัก โดย่ก�ริพิ�จ�ริณ�จ�กเงื่��อันไขทึ!�ได�

ริ'ป็แบบIf เง��อนไข Then

คำ�าสิ��ง 1Else

คำ�าสิ��ง 2End If

Page 8: SCCS031 Principle of Computer Programming

Flow-Chart : If…Then…Else Statement

condition

Statement ;Statement ;

TrueFalse

Page 9: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then..Else

ห้ากใสิ�คำ�า x เป็"น 0 แลั�วกดป็/0มีทำดสิอบ จะแสิดงข�อคำวามีว�า Equal zero แลัะ Bye

ห้ากใสิ�คำ�า อ��น จะแสิดงคำ�าว�า Not Equal Zero แลัะ Bye

Start

x

x = 0

End

Bye

Equal zero

yn

Not Equal zero

Page 10: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then..Else ให้�สิร�างห้น�าฟอร,มี ด�งน�)

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

Code ป็/0มีทำดสิอบ

Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub

Code ป็/0มีClear

Text1.Text

Text2.Text Text3.Text

Page 11: SCCS031 Principle of Computer Programming

คำ��สั่��งื่ IF…Then…Else…ซ้�อันก�นหล�ย่ช้�*น

คำ�าสิ��ง IF ซ้�อนก�นจะเก�ดข2)นเมี��อทำางเลั�อกน�)นมี�มีากกว�า 2 ทำางเลั�อกIf เง��อนไข Then

คำ�าสิ��ง 1Elseif เง��อนไข Then

คำ�าสิ��ง 2 Elseif เง��อนไข Then

คำ�าสิ��ง 3Else

คำ�าสิ��ง 4 (คำ�าสิ��งทำ��ไมี�ต่รงเง��อนไขใดเลัย)End if

Page 12: SCCS031 Principle of Computer Programming

Flow-Chart : IF…Then…Else…ซ้�อันก�นหล�ย่ช้�*น

Page 13: SCCS031 Principle of Computer Programming

เป็ริ!ย่บเทึ!ย่บริะหว��งื่ 2 ทึ�งื่เล�อัก และ ม�กกว�� 2 ทึ�งื่เล�อัก

If เงื่��อันไข Then

คำ��สั่��งื่ 1

Elseคำ��สั่��งื่ 2

End If

If เงื่��อันไข Thenคำ��สั่��งื่ 1

Elseif เงื่��อันไข Then

คำ��สั่��งื่ 2Elseif เงื่��อันไข

Thenคำ��สั่��งื่ 3

Elseคำ��สั่��งื่ 4

End if

Page 14: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then..Else ซ้�อันก�นหล�ย่ช้�*น

ป็3อนต่�วเลัข 1 จ�านวน เมี��อกดป็/0มี ทำดสิอบ

ห้ากต่�วเลัขน�)นมีากกว�า 0 ให้�แสิดงข�อคำวามีว�า More than Zero

ห้ากต่�วเลัขน�)นน�อยกว�า 0 ให้�แสิดงข�อคำวามีว�า Less than Zero

ห้ากต่�วเลัขน�)นมี�คำ�าเป็"น 0 ให้�แสิดงข�อคำวามีว�า Equal Zero

ห้ลั�งจากน�)นให้�แสิดงคำ�าว�า Bye

Start

x

x > 0

Equal zero

y n

More than Zero x < 0

Less than Zero

Bye

End

y n

Page 15: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ If..Then..Else หล�ย่ช้�*น

ให้�สิร�างห้น�าฟอร,มี ด�งน�)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

Code ป็,-มตัริวจสั่อับคำ��

Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub

Code ป็,-มClear

Private Sub Command3_Click() EndEnd Sub

Code ป็,-มClose

Text1.Text

Text1.Text

Text1.Text

Page 16: SCCS031 Principle of Computer Programming

คำ��สั่��งื่ Select Case เป็"นคำ�าสิ��งทำ��เห้มีาะสิ�าห้ร�บการต่รวจสิอบเง��อนไข

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

Page 17: SCCS031 Principle of Computer Programming

ริ'ป็แบบ คำ��สั่��งื่ Select Case

Select Case ช้��อัตั�วแป็ริทึ!�ตัริวจสั่อับเงื่��อันไขCase เงื่��อันไข 1 หริ�อั คำ��คำงื่ทึ!� 1

คำ��สั่��งื่ 1Case เงื่��อันไข 2 หริ�อั คำ��คำงื่ทึ!� 2

คำ��สั่��งื่ 2Case เงื่��อันไข 3 หริ�อั คำ��คำงื่ทึ!� 3

คำ��สั่��งื่ 3Case เงื่��อันไข n หริ�อั คำ��คำงื่ทึ!� n

คำ��สั่��งื่ nCase Else

คำ��สั่��งื่ ...( คำ��สั่��งื่ทึ!�ไม�ตัริงื่ก�บ case ใด ๆ เลย่ )

End Select

Page 18: SCCS031 Principle of Computer Programming

Flow-Chart : Select Case

conditionvalue1 …value2 value n else

S ; Statement ;

S ;Statement ;

Page 19: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ Select Case

ป็3อนต่�วเลัข 1 จ�านวน เมี��อกดป็/0มี ทำดสิอบ

ห้ากต่�วเลัขน�)นมีากกว�า 0 ให้�แสิดงข�อคำวามีว�า More than Zero

ห้ากต่�วเลัขน�)นน�อยกว�า 0 ให้�แสิดงข�อคำวามีว�า Less than Zero

ห้ากต่�วเลัขน�)นมี�คำ�าเป็"น 0 ให้�แสิดงข�อคำวามีว�า Equal Zero

ห้ลั�งจากน�)นให้�แสิดงคำ�าว�า Bye

Start

x

x

Equal zeroMore than ZeroLess than Zero

Bye

End

< 0

else> 0

Page 20: SCCS031 Principle of Computer Programming

ตั�วอัย่��งื่ก�ริใช้�คำ��สั่��งื่ Select Case

ให้�สิร�างห้น�าฟอร,มี ด�งน�)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

Code ป็,-มตัริวจสั่อับคำ��

Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub

Code ป็,-มClear

Private Sub Command3_Click() EndEnd Sub

Code ป็,-มClose

Text1.Text

Text2.Text

Text3.Text

Page 21: SCCS031 Principle of Computer Programming

ก�ริตัริวจสั่อับเกริด

Text1.Text

Text2.Text

Page 22: SCCS031 Principle of Computer Programming

Private Sub Command1_Click()Dim score As IntegerDim grade As String score = Val(Text2.Text)

ตั�วอัย่��งื่งื่�น

Page 23: SCCS031 Principle of Computer Programming

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 " & gradeEnd Sub

Page 24: SCCS031 Principle of Computer Programming

LOGO

24