Indeterminant Loops

Page last updated 04/21/99


Loop Types

The for( ) loop is generally used when we have a determinant number of iterations.  That is, we count up (or down) to the nuimber of iterations that we need using a loop control variable.

There are many situations that we need iteration, but we don't know know before hand, nor can be ascertained by the program before the loop is entered.  This is when we use the indeterminant loop.
 
 
 
 
 
 
 
 
 
 
 
 

The while Loop

The while loop is an iterative structure that repeats as long as the condition in the while loop is true.  There may or may not be a counter.  The while loop condition will have some variable in it that normally is tested in some way to drive whether the loop is repeated.
 
The syntax An example
while(condition){

   loop body

}
// powers of two up to 1000

int power = 1;

while(power<1000){

    cout << power << endl;

    power *= 2;

}

Notes

While loop Example

void moveTillStopped(grid & g)

{ // post; The mover is facing a block or edge in front

  while(g.frontIsClear()){

     g.move();

  }

}

int main()

{

   grid tarpit(5,10);

   cout << "A grid initialized only with rows and columns"<<endl 

        << "has its mover randomly set." << endl;

   moveTillStopped(tarpit);

   tarpit.display() << endl;

   return 0;

}
 

 
 
 
 
 


 
 

Using a Sentinel

A sentinel is a specially chosen value to indicate (flag) the end of a list. 99 unfortunately has been chosen as a sentinel in some old programs.  What consequences might we expect?
 
 
 
 
 
 
 
 
 
 
 
 

cin >> used as a Sentinel

cin >> variable form is generally used as a separate statement.  The statement actually can be treated as a boolean expression which means it can be used as the condition of an if( ) or as the condition of a while( ) loop.

The input statement returns true if the input successfull retrieved a value to put into the variable.  It will be false otherwise.

while((cin >> x) && (x >= 0)){

   // do something with x

}
 

 
 
 
 
 
 
 

Average of a List of Numbers

// Use a sentinel of -1 to terminate a loop

#include <iostream>

using namespace std;

int main()

{

  const double sentinel = -1.0; // User enters this to terminate

  double accumulator = 0.0;     // Maintain running sum of inputs

  int n = 0;                    // Maintain total number of inputs

  double testScore, average;



  // Prompt

  cout << "Enter test scores [0.0 through 100.0] or " << sentinel

       << " to quit" << endl;

                    // Input and process at the same time

  while( (cin >> testScore) && (testScore != sentinel) ){

    accumulator += testScore; // Update accumulator

    n++;                      // Update total inputs

  }

  if(n > 0){

    average = accumulator / n;

    cout << "Average of " << n << " tests = " << average << endl;

  } else {

    cout << "Can't average 0 numbers" << endl;

  }

  return 0;

}

 

 
 
 

Infinite Loops

It is very easy to write a loop that will not terminate.  Study the cases in the exercises and self checks for pitfalls common to infinite loops.