Sunday, April 24, 2011

Loops

The following program, in which the condition in the Do loop is "num <=7", displays the numbers from 1 through 7. (After the Do loop terminates, the value of num will be 8.)

 Public Class Form1
  
   Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
  
     'Display the number from 1 to 7
  
     Dim num As Integer = 1
  
     Do While num <= 7
  
       lstNumbers.Items.Add(num)
  
       num += 1  'add 1 to the value of num
  
     Loop
  
   End Sub
  
 End Class  

While loop keeps executing until the condition against which it tests remain true. The syntax of while loop looks like this:
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
  
 Sub Main()
  
 Dim d, e As Integer
  
 d = 0
  
 e = 6
  
 While e > 4
  
 e -= 1
  
 d += 1
  
 End While
  
 System.Console.WriteLine("The Loop ran " & e & "times")
  
 End Sub
  
 End Class  

No comments:

Post a Comment