Wednesday, February 16, 2011

Loops

Below are some examples of code for different loops used in VB.Net

Code for a do(while first) loop
 Public Class Form1
  
   Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click
  
     Dim x As Integer = 5
  
     Do While x < 10
  
       x += 1
  
     Loop
  
   End Sub
  
 End Class  

Code for a do(while last) loop
 Public Class Form1
  
   Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click
  
     Dim x As Integer = 5
  
     Do
  
       x += 1
  
       MsgBox(x)
  
     Loop While (x < 10)
  
   End Sub
  
 End Class  

Code for a for loop
 Public Class Form1
  
   Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click
  
     Dim x As Integer = 1
  
     Dim y As Integer = 5
  
     For n As Integer = x To y
  
       MsgBox(n)
  
     Next
  
   End Sub
  
 End Class  

Code For a for each loop
 Public Class Form1
  
   Private Sub btnLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoop.Click
  
     Dim name As String = "Ben"
  
     Dim character As Char
  
     For Each character In name
  
       MsgBox(character)
  
     Next
  
   End Sub
  
 End Class  

No comments:

Post a Comment