摘要:(200-06-18) VB.NET 教學 次數廻圈
次數廻圈
'次數迴圈
Module TestForNext
Public Sub Main()
'定義區域變數
Dim i1 As Int32
For i1 = 1 To 9
System.Console.WriteLine(i1)
'跳出
If (i1 = 5) Then
'跳出這一個迴圈
Exit For
End If
'i1自動累計1
Next
System.Console.WriteLine(i1)
For i2 As Int32 = 1 To 10 Step 2
System.Console.WriteLine(i2)
Next
'System.Console.WriteLine(i2) i2生命週期沒有到這裡
End Sub
End Module
巢狀次數廻圈
'巢狀迴圈
Module TestNestedFor
'主程式
Public Sub Main()
'上層迴圈
outer:
For i As Int32 = 1 To 9
'內圈
inner:
For k As Int32 = 1 To 9
System.Console.WriteLine(i.ToString() + " X " + k.ToString() + " = " + (i * k).ToString())
'判斷
If (k = 5) Then
Exit For '最近那一回圈
End If
Next
Next
End Sub
End Module
條件廻圈
'條件式迴圈
Module TestWhile
Public Sub Main()
'無窮盡迴圈
Dim i1 As Int32 = 0
While (True)
i1 += 1 'i1=i1+1
System.Console.WriteLine(i1.ToString())
'跳出去
If (i1 = 100) Then
Exit While
End If
End While
'進行比對
Dim r1 As Int32 = 0
While (r1 <= 100)
System.Console.WriteLine(r1.ToString())
'遞增作業
r1 += 1
End While
End Sub
End Module
DoWhile
Module TestDoWhile
Public Sub Main()
'先執行後判斷
Dim i1 As Int32 = 100
Do
System.Console.WriteLine(i1.ToString())
Loop While (i1 < 100)
End Sub
End Module