CS110 Lab: While Loop


We will examine how Boolean expressions can be used in a while statement. A while statement makes a program repeats a statement or group of statements. Such repetitions are called loops. This web page leads helps to you to master different types of while loops:

A while loop

The syntax of the while loop is:
while ( Expression )

{
Statement
}
A while statement allows a program to continue executing a statement as long as the value of the Boolean expression is true. When the Boolean expression is evaluated as false, execution of the program continues with the statement immediately following the while statement.

Count-controlled while loop

Now let's look at the following C++ code:
sum = 0;
count = 1;
while (count <= 10)
{
	cout << "Enter a value: ";
	cin >> value;
	sum = sum + value;
	count++;
}

cout << "The sum of the 10 numbers is " 
     << sum << endl;
The variables sum and count are assigned the values 0 and 1 respectively.
The Boolean expression (count <= 10) is evaluated. Because the value in count is less than or equal to 10, the expression is true and the compound statement (block) associated with the While statement is executed. A number is extracted from the standard input stream and added to the sum.
The value in count is incremented by 1.

The While expression is evaluated again. Because the value stored in the count is still less than 10, the compound satatement associated with the While statement is executed again. This process countinues until count contains the value 11. At that time, the expression is no longer true, the body of the While loop is not executed again, and the execution continues with the statement immediately following the While statement. In this case, it continues with a cout that sends the labeled answer to the output stream.

Count-controlled while loop

A count-controlled loop is one that is executed a certain number of times. The above example shows a count-controlled loop. We know that the program sends the answer to the output stream after excuting the loop body 10 times. Now let's look at another loop control structure --- event-controlled loop.

Back to the top

Event-controlled loop

An event-controlled loop is one whose execution is controlled by the occurence of an event whithin the loop itself. Now let's look at an example of an event-controlled loop that reads and sums value from cin until a negative value is encountered.
sum = 0;
cout << "Enter a value: ";
cin >> value;

while (value >= 0)
{
   sum = sum + value;
   cout << "Enter a value: ";
   cin >> value;

}
cout << "The sum of the values prior to a negative value is "
     << sum << endl;

For this same problem, we can write a program in another way:
sum = 0;
cout << "Enter a value: ";
cin >> value;

// Set the boolean variable moreData to true if the first data item is 
// not negative;  false otherwise.

if (value >= 0)
   moreData = true;
else
   moreData = false;

while (moreData)
{
   sum = sum + value;
   cout << "Enter a value: ";
   cin >> value;

   // Reset the value of moreData
   if (value >= 0)
      moreData = true;
   else
      moreData = false;

}
cout << "The sum of the values prior to a negative value is "
     << sum << endl;

sum is set to zero and the first data item value is read outside of the loop. value is compared to zero and its result is stored in the Boolean variable moreData. If the first data item is negative, moreData has a value of false, and the body of the loop is not executed. If the first data item is non-negative, moreData has a value of true, and the body of the loop is entered and executed. In the later case, value is added to the sum and the next data item is read. This new data item is compared to zero again, and the variable moreData is reset. The expression is tested again. This process continues until a value of negative is read and moreData becomes false. When this happens, the body of the While loop is not executed again. The sum of the non-negative numbers is sent to the output stream.

The above while loop is also called a flag-controlled loop. The variable moreData is the flag. It has the equivalent effect as the following:

sum = 0;
cout << "Enter a value: ";
cin >> value;

// Set moreData to true if the first data item is not negative;
// false otherwise.

moreData = value >= 0;

while (moreData)
{
   sum = sum + value;
   cout << "Enter a value: ";
   cin >> value;

   // Reset the value of moreData
   moreData = value >= 0;
}
cout << "The sum of the values prior to a negative value is "
     << sum << endl;

Proper Loop Operation

For obvious reasons, the While statement is called a loop or looping statement. The statement that is being executed within the loop is called the body of the loop.

There are three basic steps that must be done for a loop to operate properly.

  1. The variables in the expression (the counter or event) must be set (initialized) before the while statement is executed the first time.
  2. The expression must test the counter or event correctly so that the body of the loop executes when it is supposed to and terminates at the proper time.
  3. The counter or the status of the event must be updated within the loop. If the counter or the status of the event is not updated, the loop never stops executing. This situation is called an infinite loop.

Back to the top



Copyright: Department of Computer Science, University of Regina.