<< Chapter < Page Chapter >> Page >

The undesirable semi-colon on the end of while line causes the action of the while loop to be the "nothingness" between the closing parenthesis and the semi-colon. The program will infinitely loop because there is no action (that is no action and no update). If this is the first item in your program it will appear to start but there will be no output.

Counting loops

The examples above are for an event controlled loop. The flag updating is an event where someone decides if they want the loop to execute again. Often the initialization sets the flag so that the loop will execute at least once.

Another common usage of the while loop is as a counting loop. Consider:

C++ source code: while loop that is counting

counter = 0; while (counter<5) {cout<<"\nI love ice cream!"; counter++;}

Got questions? Get instant answers now!

The variable counter is said to be controlling the loop.  It is set to zero (called initialization) before entering the while loop structure and as long as it is less than 5 (five); the loop action will be executed.  But part of the loop action uses the increment operator to increase counter's value by one.  After executing the loop five times (once for counter's values of: 0, 1, 2, 3 and 4) the expression will be false and the next line of code in the program will execute. A counting loop is designed to execute the action (which could be more than one statement) a set of given number of times. In our example, the message is displayed five times on the monitor. It is accomplished my making sure all four attributes of the while control structure are present and working properly. The attributes are:

  • Initializing the flag
  • Test expression
  • Action or actions
  • Update of the flag

Missing an attribute might cause an infinite loop or give undesired results (does not work properly).

Infinite loops

Consider:

C++ source code: infinite loop

counter = 0; while (counter<5) {cout<<"\nI love ice cream!"; }

Got questions? Get instant answers now!

Missing the flag update usually causes an infinite loop.

Variations on counting

In the following example, the integer variable age is said to be controlling the loop (that is the flag). We can assume that age has a value provided earlier in the program. Because the while structure is a test before loop; it is possible that the person’s age is 0 (zero) and the first time we test the expression it will be false and the action part of the loop would never be executed.

C++ source code: while as a counting loop

while (0<age) {cout<<"\nI love candy!"; age--;}

Got questions? Get instant answers now!

Consider the following variation assuming that age and counter are both integer data type and that age has a value:

C++ source code: while as a counting loop

counter = 0; while (counter<age) {cout<<"\nI love corn chips!"; counter++;}

Got questions? Get instant answers now!

This loop is a counting loop similar to our first counting loop example. The only difference is instead of using a literal constant (in other words 5) in our expression, we used the variable age (and thus the value stored in age) to determine how many times to execute the loop. However, unlike our first counting loop example which will always execute exactly 5 times; it is possible that the person’s age is 0 (zero) and the first time we test the expression it will be false and the action part of the loop would never be executed.

Definitions

while
A test before iteration control structure available in C++.
loop attributes
Items associated with iteration or looping control structures.
initialize item
An attribute of iteration control structures.
might not happen
Indicating that test before loops might not execute the action.
event controlled
Using user input to control a loop.
counting controlled
Using a variable to count up or down to control a loop.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask