150 likes | 249 Vues
Learn about the Select...Case statement in VB, control program flow with specific conditions, and optimize your coding techniques. Explore practical examples and understand its usage with Select...Case for efficient programming.
E N D
Select ..Case คำสั่ง SelectCaseเป็นคำสั่งสำหรับสร้างเงื่อนไขการทำงานมีการทำงานเหมือนกับคำสั่ง if แต่จะมีการเปรียบเทียบข้อมูลที่เจาะจง Select Case ตัวแปร Case เงื่อนไข 1 คำสั่ง Case เงื่อนไข 2 ……. Case Else ‘– Case Else จะมีหรือไม่มีก็ได้ คำสั่งสำหรับกรณีอื่นๆ End Select
Select ..Case ตัวอย่าง 1 Dim a as String a=“T” Select Case a Case “T” MessageBox.Show(“T”) Case “B” MessageBox.Show(“B”) Case Else MessageBox.Show(“Not T B”) End Select
Select ..Case ตัวอย่าง 2 Dim a as String a=“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
Select ..Case ตัวอย่าง 3 Dim a as Integer a=70 Select Case a Case Is >=80 MessageBox.Show(“A”) Case Is >=70 MessageBox.Show(“B”) Case Else MessageBox.Show(“F”) End Select
Select ..Case ตัวอย่าง 3 Dim a as Integer a=12 Select Case a Case 0 To 2 MessageBox.Show(“วัยทารก”) Case 3To 12 MessageBox.Show(“วัยเด็ก”) Case Else MessageBox.Show(“วัยรุ่น”) End Select
คำสั่ง For For เป็นคีย์เวิร์ดสำหรับกำหนดการทำงานแบบวงรอบหรือลูป จากค่าเริ่มต้น จนถึงค่าสุดท้ายขอตัวที่ใช้เป็นตัวนับ โดยที่ตัวนับไม่จำเป็นต้องเริ่มต้นจาก 0 และอาจเป็นการนับแบบเพิ่มค่าหรือลดค่าก็ได้รูปแบบคือ For ตัวนับ = ค่าเริ่มต้น To ค่าสุดท้าย คำสั่ง Next
คำสั่ง For ตัวอย่าง Dim i, sum as integer For i=1 To 10 sum+=i MessageBox.Show(i) Next
คำสั่ง For ตัวอย่างการเพิ่มลดค่า Dim i, sum as integer For i=10 To 100Step 10 sum+=i MessageBox.Show(i) Next
คำสั่ง For ตัวอย่างการเพิ่มลดค่า Dim i, sum as integer For i=100 To 10Step -10 sum+=i MessageBox.Show(i) Next
คำสั่ง While While เป็นคีย์เวิร์ดสำหรับกำหนดการทำงานแบบวงรอบหรือลูป โดยค่าเริ่มต้น จะถูกส่งเพียงครั้งเดียว แล้วทำตามเงื่อนไขของ while ซึ่งการทำงานจะถูกตรวจสอบก่อนถ้าตรงตามเงื่อนไขถึงให้ทำงานได้ Whileเงื่อนไข คำสั่ง End While
คำสั่ง While ตัวอย่าง Dim n As Integer =0 While (n<=10) n+=1 MessageBox.Show(n) End While
คำสั่ง Do..While Do While เป็นคีย์เวิร์ดสำหรับกำหนดการทำงานแบบวงรอบหรือลูป โดยค่าเริ่มต้น จะถูกส่งเพียงครั้งเดียว แล้วทำตามเงื่อนไขของ while ซึ่งการทำงานจะถูกตรวจสอบหลังจากการทำงานแล้ว จากนั้นจึงตรวจสอบเงื่อนไข ถ้าผ่านถึงให้ทำงานต่อได้ Do คำสั่ง Loop While เงื่อนไข
คำสั่ง Do While ตัวอย่าง Dim n As Integer =0 do n+=1 MessageBox.Show(n) Loop While (n<=10)
คำสั่ง IsNumeric() คำสั่งในการตรวจสอบตัวเลข Dim n As Integer if(IsNumeric(TextBox1.Text)) Then n=TextBox1.Text MessageBox.Show(“OK”) Else MessageBox.Show(“NO”) End if