Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
While Loops

CS110 Lab: While Loop


Video Summary: https://youtu.be/VKHUAiT1jzQ


We will examine how Boolean expressions can be used in a while statement. A while statement makes a program repeat a statement or group of statements (block). Such repetitions are called loops. This web page will help you to master different types of while loops. Contents:

while Loop Syntax

The syntax of the while loop is:

      
         while ( Boolean expression )
         {
            //This will repeat while the Boolean expression is true
         }
      
   

A while statement allows a program to continue executing a statement as long as the value of the Boolean expression is true. The Boolean expression is first tested when execution first reaches the while.
If it is true, the following statement or block will run once, and the Boolean expression will be tested again. This continues until the Boolean expression evaluates to false.
When it is false, execution of the program continues with the statement immediately following the while statement.

 

Count-controlled while Loop

A count-controlled loop is one that is executed a certain number of times. The example below shows a count-controlled loop that will provide the the answer to the output stream after excuting the loop body 10 times.

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.

When the block of statements ends, the while expression is evaluated again. Because the value stored in the count is still less than 10, the block 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.

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.

For this same problem, we can write a program in another way:

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:

Back to the top

 

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. initialize: The variables in the expression (the counter or event) must be set (initialized) before the while statement is executed the first time.
  2. compare: 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. update: 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.

Escaping Infinite Loops

If you find yourself in an infinite loop, you will need to stop the operation of the program. This is done by either clicking stop or pressing ctrl + c in the console.

If you create an infinite loop in Replit, the program will experience severe lag. After stopping the program, you may need to wait a bit or refresh the page and wait until the site is back to normal.