- Introduction
- Control Structures
- If/Then Selection Structure
- If/Then/Else Selection Structure
- While Repetition Structure
- DoWhile/Loop Repetition Structure
- DoUntil/Loop Repetition Structure
- Do/LoopWhile Repetition Structure
- Do/LoopUntil Repetition Structure
- Assignment Operators
- For/Next Repetition Structure
- Example: Using the For/Next Structure to Compute Compound Interest
- SelectCase Multiple-Selection Structure
- Using the Exit Keyword in a Repetition Structure
- Logical Operators
- Introduction to Windows Application Programming
- Summary
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 structuresone 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.]