Sunday, April 17, 2011

Blog: Loops

1.) Do (While First) Loop
 Module Module1
  
   Sub Main()
  
      ' Locals used in Do While loop.
  
      Dim i As Integer = 100
  
      Dim z As Integer = 0
  
      ' Loop.
  
      Do While i >= 0 And z <= 20
  
        Console.WriteLine("i = {0}, z = {1}", i, z)
  
        i = i - 10
  
        z = z + 3
  
      Loop
  
   End Sub
  
 End Module
  

2.) Do (While Last) Loop
 Public Class MainClass
  
   Shared Sub Main(ByVal args As String())
  
     Dim counterVariable As Integer = 100
  
     Do
  
      Console.WriteLine("counterVariable: {0}", counterVariable)
  
      counterVariable = counterVariable + 1
  
     Loop While counterVariable < 10
  
   End Sub
  
 End Class
  

3.) For Loop
 Module Module1
  
   Sub Main()
  
      ' This loop goes from 0 to 5.
  
      For value As Integer = 0 To 5
  
        ' Exit condition if the value is three.
  
        If (value = 3) Then
  
           Exit For
  
        End If
  
        Console.WriteLine(value)
  
      Next
  
   End Sub
  
 End Module
  

4.) For Each Loop
 Module Module1
  
   Sub Main()
  
      ' The input array.
  
      Dim ferns() As String = {"Psilotopsida", _
  
                      "Equisetopsida", _
  
                      "Marattiopsida", _
  
                      "Polypodiopsida"}
  
      ' Loop over each element with For Each.
  
      For Each fern As String In ferns
  
        Console.WriteLine(fern)
  
      Next
  
   End Sub
  
 End Module
  

No comments:

Post a Comment