VB.Net/Language Basics/For
Содержание
Decimal For Loop Controlling Variable
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim loopCounter As Single For loopCounter = 0.5 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub
End Class
</source>
Exit from For
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim counter As Integer For counter = 1 To 10 " skip remaining code in loop only if counter = 3 If counter = 3 Then Exit For End If Next Console.WriteLine("counter = " & counter & _ " after exiting For/Next structure" & vbCrLf) End Sub
End Class
</source>
For Loop Demo
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() "Count from 1 to 10 For intCount As Integer = 1 To 10 System.Console.WriteLine(intCount) Next "Count backwards from 10 to 1 For intCount As Integer = 10 To 1 Step -1 System.Console.WriteLine(intCount) Next End Sub
End Class
</source>
For Loop With Decimal Step
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim loopCounter As Single For loopCounter = 0.5 To 9 Step 0.5 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub
End Class
</source>
For Loop with default Step
<source lang="vbnet"> Imports System public class MainClass
Shared Sub Main() Dim loopCounter As Integer For loopCounter = 0 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub
End Class
</source>
For loop with float init value
<source lang="vbnet"> Imports System public class MainClass
Shared Sub Main() Dim loopCounter As Single For loopCounter = 0.5 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub
End Class
</source>
For Loop with Float step
<source lang="vbnet"> Imports System public class MainClass
Shared Sub Main() Dim loopCounter As Single For loopCounter = 0.5 To 9 Step 0.5 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub
End Class
</source>
For Loop with negative step
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() For intCount As Integer = 10 To 1 Step -1 "Add the item to the list System.Console.WriteLine(intCount) Next End Sub
End Class
</source>
For loop with step
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() "Perform a loop For intCount As Integer = 4 To 62 Step 7 "Add the item to the list System.Console.WriteLine(intCount) Next End Sub
End Class
</source>
Nested For Loop
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main(ByVal args As String()) Dim outer As Integer Dim inner As Integer For outer = 3 To 6 For inner = 10 To 12 Console.WriteLine("{0} * {1} = {2}", _ outer, inner, outer * inner) Next inner, outer End Sub
End Class
</source>
Simplest For loop
<source lang="vbnet"> Imports System Public Class MainClass
Shared Sub Main() Dim intCount As Integer "Perform a loop For intCount = 1 To 5 "Add the item to the list System.Console.WriteLine("I am item " & intCount & " in the list!") Next End Sub
End Class
</source>