<< Chapter < Page | Chapter >> Page > |
There are two commonly used test before loops in the iteration (or repetition) category of control structures. They are: while and for. This module covers the: while.
The concept of iteration is connected to possibly wanting to repeat an action. Like all control structures we ask a question to control the execution of the loop. The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the while loop is as follows:
initialization of the flag
while the answer to the question is true then dosome statements or action
some statements or actionsome statements or action
update the flag
In almost all languages the question (called a test expression ) is a Boolean expression . The Boolean data type has two values – true and false. Let's rewrite the structure to consider this:
initialization of the flag
while the expression is true then dosome statements or action
some statements or actionsome statements or action
update the flag
Within the while control structure there are four attributes to a properly working loop. They are:
The initialization of the flag is not technically part of the control structure, but a necessary item to occur before the loop is started. The English phrasing is, "While the expression is true, do the following actions". This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test before loop the action might not happen . It is called a test before loop because the test comes before the action. It is also sometimes called a pre-test loop, meaning the test is pre (or Latin for before) the action and update.
Consider the following one-way conversation from a mother to her child.
Child: The child says nothing, but mother knows the child had Cheerios for breakfast and history tells us that the child most likely spilled some Cheerios on the floor.
Mother says: "While it is true that you see (As long as you can see) a Cheerio on floor, pick it up and put it in the garbage."
Note: All of the elements are present to determine the action (or flow) that the child will be doing (in this case repeating). Because the question (can you see a Cheerios) has only two possible answers (true or false) the action will continue while there are Cheerios on the floor. Either the child 1) never picks up a Cheerio because they never spilled any or 2) picks up a Cheerio and keeps picking up Cheerios one at a time while he can see a Cheerio on the floor (that is until they are all picked up).
The syntax for the while control structure within the C++ programming language is:
statement; // This statement initializes the flag;
while (expression){
statement;statement;
statement;statement; // This statement updates the flag;
}
Notification Switch
Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?