Control Statements

15
Control Statements

description

Control Statements. Select ..Case. คำสั่ง Select Case เป็น คำสั่งสำหรับสร้างเงื่อนไขการ ทำงาน มีการทำงานเหมือน กับคำสั่ง if แต่จะมีการเปรียบเทียบข้อมูลที่เจาะจง. Select Case ตัวแปร Case เงื่อนไข 1 คำสั่ง Case เงื่อนไข 2 ……. - PowerPoint PPT Presentation

Transcript of Control Statements

Page 1: Control Statements

Control Statements

Page 2: Control Statements

Select ..CaseSelect ..Case

คำ��สั่��ง Select Case เป็�นคำ��สั่��งสั่��หรั�บสั่รั��งเง��อนไขก�รัทำ��ง�นมี�ก�รัทำ��ง�นเหมี�อน ก�บคำ��สั่��ง if แต่�จะมี�ก�รัเป็รั�ยบเทำ�ยบข�อมี�ลทำ��เจ�ะจง Select Case ต่�วแป็รั Case เง��อนไข 1

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

…….Case Else ‘– Case

Else จะมี�หรั�อไมี�มี�ก"ได้�คำ��สั่��งสั่��หรั�บกรัณี�อ��นๆ

End Select

Page 3: Control Statements

Select ..CaseSelect ..Case

ตั�วอย่�าง 1Dim a as Stringa=“T”

Select Case a Case “T”

MessageBox.Show(“T”)Case “B”

MessageBox.Show(“B”)Case Else

MessageBox.Show(“Not T B”)End Select

Page 4: Control Statements

Select ..CaseSelect ..Case

ตั�วอย่�าง 2Dim a as Stringa=“T”

Select Case a Case “T”,”Z”,”X”

MessageBox.Show(“T Z X”)Case “B”

MessageBox.Show(“B”)Case Else

MessageBox.Show(“Not T Z X B”)

End Select

Page 5: Control Statements

Select ..CaseSelect ..Case

ตั�วอย่�าง 3Dim a as Integera=70

Select Case a Case Is >=80

MessageBox.Show(“A”)Case Is >=70

MessageBox.Show(“B”)Case Else

MessageBox.Show(“F”)End Select

Page 6: Control Statements

Select ..CaseSelect ..Case

ตั�วอย่�าง 3Dim a as Integera=12

Select Case a Case 0 To 2

MessageBox.Show(“ว�ยทำ�รัก”)Case 3 To 12

MessageBox.Show(“ว�ยเด้"ก”)Case Else

MessageBox.Show(“ว�ยรั& �น”)End Select

Page 7: Control Statements

คำ�าสั่� ง Forคำ�าสั่� ง For

For เป็�นคำ�ย'เว(รั'ด้สั่��หรั�บก��หนด้ก�รัทำ��ง�นแบบวงรัอบหรั�อล�ป็ จ�กคำ��เรั(�มีต่�น จนถึ*งคำ��สั่&ด้ทำ��ยขอต่�วทำ��ใช้�เป็�นต่�วน�บ โด้ยทำ��ต่�วน�บไมี�จ��เป็�นต่�องเรั(�มีต่�นจ�ก 0 และอ�จเป็�นก�รัน�บแบบเพิ่(�มีคำ��หรั�อลด้คำ��ก"ได้�รั�ป็แบบคำ�อ For ต่�วน�บ = คำ��เรั(�มีต่�น To คำ��สั่&ด้ทำ��ย

คำ��สั่��งNext

Page 8: Control Statements

คำ�าสั่� ง Forคำ�าสั่� ง For

ตั�วอย่�างDim i, sum as integerFor i = 1 To 10

sum+=i MessageBox.Show(i)

Next

Page 9: Control Statements

คำ�าสั่� ง Forคำ�าสั่� ง For

ตั�วอย่�าง การเพิ่� มลดคำ�าDim i, sum as integerFor i = 10 To 100 Step 10

sum+=i MessageBox.Show(i)

Next

Page 10: Control Statements

คำ�าสั่� ง Forคำ�าสั่� ง For

ตั�วอย่�าง การเพิ่� มลดคำ�าDim i, sum as integerFor i = 100 To 10 Step -10

sum+=i MessageBox.Show(i)

Next

Page 11: Control Statements

คำ�าสั่� ง Whileคำ�าสั่� ง While

While เป็�นคำ�ย'เว(รั'ด้สั่��หรั�บก��หนด้ก�รัทำ��ง�นแบบวงรัอบหรั�อล�ป็ โด้ยคำ��เรั(�มีต่�น จะถึ�กสั่�งเพิ่�ยงคำรั�/งเด้�ยว แล�วทำ��ต่�มีเง��อนไขของ while ซึ่*�งก�รัทำ��ง�นจะถึ�กต่รัวจสั่อบก�อนถึ��ต่รังต่�มีเง��อนไขถึ*งให�ทำ��ง�นได้� While เง��อนไข

คำ��สั่��งEnd While

Page 12: Control Statements

คำ�าสั่� ง Whileคำ�าสั่� ง While

ตั�วอย่�าง

Dim n As Integer =0 While (n<=10)

n+=1MessageBox.Show(n)

End While

Page 13: Control Statements

คำ�าสั่� ง Do..Whileคำ�าสั่� ง Do..While

Do While เป็�นคำ�ย'เว(รั'ด้สั่��หรั�บก��หนด้ก�รัทำ��ง�นแบบวงรัอบหรั�อล�ป็ โด้ยคำ��เรั(�มีต่�น จะถึ�กสั่�งเพิ่�ยงคำรั�/งเด้�ยว แล�วทำ��ต่�มีเง��อนไขของ while ซึ่*�งก�รัทำ��ง�นจะถึ�กต่รัวจสั่อบหล�งจ�กก�รัทำ��ง�นแล�ว จ�กน�/นจ*งต่รัวจสั่อบเง��อนไข ถึ��ผ่��นถึ*งให�ทำ��ง�นต่�อได้� Do

คำ��สั่��งLoop While เง��อนไข

Page 14: Control Statements

คำ�าสั่� ง Do Whileคำ�าสั่� ง Do While

ตั�วอย่�าง

Dim n As Integer =0 do

n+=1MessageBox.Show(n)

Loop While (n<=10)

Page 15: Control Statements

คำ�าสั่� ง IsNumeric()คำ�าสั่� ง IsNumeric()

คำ�าสั่� งในการตัรวจสั่อบตั�วเลข

Dim n As Integer if(IsNumeric(TextBox1.Text)) Then

n=TextBox1.TextMessageBox.Show(“OK”)

ElseMessageBox.Show(“NO”)

End if