標題:
VB.NET 教學 廻圈
[打印本頁]
作者:
narita
時間:
2011-9-6 00:43
標題:
VB.NET 教學 廻圈
'次數迴圈
02 Module TestForNext
03 Public Sub Main()
04 '定義區域變數
05 Dim i1 As Int32
06 For i1 = 1 To 9
07 System.Console.WriteLine(i1)
08 '跳出
09 If (i1 = 5) Then
10 '跳出這一個迴圈
11 Exit For
12 End If
13 'i1自動累計1
14 Next
15 System.Console.WriteLine(i1)
16 For i2 As Int32 = 1 To 10 Step 2
17 System.Console.WriteLine(i2)
18 Next
19 'System.Console.WriteLine(i2) i2生命週期沒有到這裡
20 End Sub
21 End Module
巢狀次數廻圈
view sourceprint?01 '巢狀迴圈
02 Module TestNestedFor
03 '主程式
04 Public Sub Main()
05 '上層迴圈
06 outer:
07 For i As Int32 = 1 To 9
08 '內圈
09 inner:
10 For k As Int32 = 1 To 9
11
12 System.Console.WriteLine(i.ToString() + " X " + k.ToString() + " = " + (i * k).ToString())
13 '判斷
14 If (k = 5) Then
15 Exit For '最近那一回圈
16 End If
17 Next
18 Next
19 End Sub
20 End Module
條件廻圈
view sourceprint?01 '條件式迴圈
02 Module TestWhile
03 Public Sub Main()
04 '無窮盡迴圈
05 Dim i1 As Int32 = 0
06 While (True)
07 i1 += 1 'i1=i1+1
08 System.Console.WriteLine(i1.ToString())
09 '跳出去
10 If (i1 = 100) Then
11 Exit While
12 End If
13 End While
14
15 '進行比對
16 Dim r1 As Int32 = 0
17 While (r1 <= 100)
18 System.Console.WriteLine(r1.ToString())
19 '遞增作業
20 r1 += 1
21 End While
22 End Sub
23 End Module
DoWhile
view sourceprint?1 Module TestDoWhile
2 Public Sub Main()
3 '先執行後判斷
4 Dim i1 As Int32 = 100
5 Do
6 System.Console.WriteLine(i1.ToString())
7 Loop While (i1 < 100)
8 End Sub
9 End Module
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)