Parents customarily remind their children not to repeat themselves . Indeed, parents often illustrate another saying ( Do as I say, not as I do ) by continually repeating that reminder.
This is my nifty way of introducing the idea that, in the world of computers, sometimes you want your code to repeat itself, too. For example, if the user enters invalid data, you may want to ask the user whether they want to retry or quit. If they retry and still enter invalid data, you again would ask the user whether they want to retry or quit. This process keeps repeating until the user either enters valid data or quits.
You use a loop to repeat the execution of code statements. A loop in C++ is a structure that repeats the execution of code until a condition becomes false. In the preceding example, the condition is that the data is invalid and the user wants to retry, thus the repeating code is the prompt asking the user whether they want to retry or quit.
This chapter will show you how to use one type of loop: the for loop. However, before discussing the for loop, I ll show you how to use increment and decrement operators, which are used in for and other types of loops . The next chapter will then show you how to use two other kinds of loops: the while loop and the do while loop.