- If Statement
- Select Case
- For Next Loop/Exiting For Loop/Continue For
- Do Until Loop
- Do While Loop
If...Then...Else
If 條件1 [ Then ] 若條件1為真,會執行此處的陳述式 [ ElseIf 條件2 [ Then ] 若條件2為真,會執行此處的陳述式 ] [ Else [ 不符以上條件才執行此處的陳述式 ] ] End If
程式範例
Dim score As Integer = 0
Console.WriteLine("請輸入分數:")
score = Console.ReadLine()
If score >= 60 Then
Console.WriteLine("及格")
ElseIf score < 60 & score >= 40 Then
Console.WriteLine("活當")
Else
Console.WriteLine("死當")
End If
Select Case
Select [ Case ] 變數名稱
[ Case 列舉或條件 [ 符合,執行此處的陳述式 ] ] [ Case Else [ 以上都不符,執行此處的陳述式 ] ]End Select
case可以使用的方法如下,只要逗點就可以把該case的條件一值加下去
程式範例 Dim MyInt As Integer = Nothing Console.WriteLine("Plz Enter an integer:") MyInt = Console.ReadLine() Select Case MyInt Case 0 Console.WriteLine("Hello 0") Case 1 To 9 Console.WriteLine("Hello 1~9") Case 10, 11, 12 Console.WriteLine("Hello 10,11,12") Case Else Console.WriteLine("Hello Else") End Select
- Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber
- Case的資料型態在2010支援Boolean、Byte、Char、Date、Double、 Decimal、 Integer、Long、Object、SByte、Short、Single、String、UInteger、ULong 和 UShort
For...Next StatementFor 變數 [ As datatype ] = 開始數值 To 結束數值 [ Step 間隔 ] [ 陳述式 ] [ Continue For ] [ 陳述式 ] [ Exit For ] [ 陳述式 ] Next [ 變數 ]程式範例Dim Int1 As Integer For Int1 = 1 To 20 Console.WriteLine("it's " & Int1) Next Console.WriteLine() For Int1 = 1 To 20 Step 2 '執行1,3,5,7,9...,19 Console.WriteLine("(step:2)it's " & Int1) Next Console.WriteLine() For Int1 = 20 To 1 Step -2 If Int1 = 2 Then '若Int1 = 2跳出整個ForLoop Exit For End If If Int1 = 5 Then '若Int1 = 5跳過底下剩餘程式,直接進入下一次回圈 Continue For End If Console.WriteLine("(step:-2)it's " & Int1) NextDo ... Loop
Do { While | Until } 條件 [ 陳述式] [ Exit Do ]==>若符合IF條件就可以離開該DO Loop [ 陳述式 ] Loop -or- Do [ 第一次一定執行陳述式 ][ Exit Do ]==>若符合IF條件就可以離開該DO Loop[ 陳述式 ] Loop { While | Until } 條件While:條件為真執行下一個Loop
Until :條件為真跳出Loop
程式範例Dim check As Boolean = True Dim counter As Integer = 0 Do Do While counter < 20 counter += 1 If counter = 10 Then check = False Exit Do End If Loop Loop Until check = False
回VB文章列表
沒有留言:
張貼留言
歡迎留言討論,沒有登入也可以留言