Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Use while loops and specify your exit condition. Here is the format for the while loop:
while (<replaceable>condition</replaceable>){
CODE
}As long as the condition is a value other than zero/nil/NULL, the while loop will run.
The while loop is the arrogant brother of the for loop (see Recipe 1.10) because while loops only take a condition that should be met for the loop to run. If the condition is positive, the loop will always run until the condition becomes negative. For instance, a while loop could be implemented to make an icon in the Dock in Mac OS X jump up and down until the user taps on that icon (this is actually a very bad user experience; icons in the Dock shouldn’t jump up and down continuously, but for a short interval, or even a fixed number of times, usually 3). The exit condition for the while loop is the user’s tapping on that icon. As long as the user hasn’t tapped on the icon, the icon will jump up and down.