Loop Structure


Program loops are used whenever you want to have one or more program statements executed one or more times. Regardless of the loop structure you use, all loops have three things in common:

  • One or more variables must be initialized to some starting value. These variables may be initialized to their starting value before the loop begins execution, or they may be initialized as part of the loop structure itself.

  • At least one of the variables must be tested at some point in the loop to decide whether another pass through the loop is to be made. This variable is often referred to as the loop control variable.

  • The loop control variable is manipulated in some way, usually an increment operation, on each pass through the loop.

If these three conditions are met, the loop at least has a chance of being a well-behaved loop. If the conditions are not met, it's very unlikely that the loop will be well behaved.

What is the likely behavior of a loop if the conditions aren't met? As a broad generalization, if the loop doesn't initialize at least one variable properly, there are two likely possibilities. First, it's possible that the statements within the loop may not be executed at all. This often happens when the first condition mentioned earlier is not met.

A second possibility is that the loop executes forever. Loops of this type are called infinite loops and are not usually desirable (exceptions might include fire and burglar alarms that continually loop through a list of sensors). Infinite loops are almost always caused by violating the second condition and third conditions.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net