VB.Net/Language Basics/For

Материал из VB Эксперт
Перейти к: навигация, поиск

Decimal For Loop Controlling Variable

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


Exit from For

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


For Loop Demo

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


For Loop With Decimal Step

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


For Loop with default Step

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


For loop with float init value

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


For Loop with Float step

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


For Loop with negative step

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


For loop with step

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


Nested For Loop

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


Simplest For loop

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