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:
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. |