Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

3.5 While Repetition Structure

A repetition structure allows the programmer to specify that an action be repeated a number of times, depending on the value of a condition. Visual Basic .NET provides seven repetition structures—one of which is the While repetition structure.

As an example of a While structure, consider a program segment designed to find the first power of two larger than 1000. Suppose Integer variable product contains the value 2. When the following While structure finishes executing, product contains the result:

Dim product As Integer = 2 

While product <= 1000
     product = product * 2 
End While 

When the While structure begins executing, product is 2. Variable product is multiplied by 2 repeatedly, taking on the values 4, 8, 16, 32, 64, 128, 256, 512 and 1024, successively. When product becomes 1024, the condition product<=1000 in the While structure becomes false. This condition causes the repetition to terminate, with 1024 as product's final value. Execution continues with the next statement after the While structure. [Note: If a While structure's condition is initially false, the body state-ment(s) will never be executed.]

  • + Share This
  • 🔖 Save To Your Account