event and counter controlled loops

The Count-Controlled while loop

In this while loop an action is repeated a given number of times. A counter variable is created and initialized to a starting value before the loop is started. The condition that is tested before each iteration of the loop is whether the counter has reached a predetermined value. The counter itself is incremented inside the while loop. In this example the counter, loopCount, is initialized to 1 and incremented in the loop until its' value is 10. You could also initialize loopCount to 10 then each time through the loop decrement it (loopCount--) testing (loopCount > 0).
 // Count controlled loop
 #include<iostream>
 using namespace std;

 int main()
 {
  int loopCount;  // loop control variable
  loopCount = 1;
  while(loopCount <= 10)
  {
   cout << "Hello! Loop count = " << loopCount << endl;
   loopCount++; // Increment control variable
  }
  return 0;
 }

The Event-Controlled while loop

In this while loop an action is repeated until a certain event occurs. This is by far the most frequently used form of the while loop. There are three different types of Event-Controlled while loops.
  1. Sentinel-controlled loops - A sentinel variable is initialized to a specific value. The while loop continues until, through some action inside the loop, the sentinel variable is set to a predefined termination value. In the following example the user is asked to type characters at the keyboard, which are then echoed to the screen, then press the Enter key when done. Pressing the Enter key sets the char variable inChar to the newline character (defined by the special characer '\n').
     
     // Sentinel-controlled loop
     #include<iostream>
     using namespace std;
    
     int main()
     {
      char inChar;   // Input variable
      cin.get(inChar);  // Init sentinel variable
      while(inChar != '\n')
      {
       cout << inChar;  // Print the character
       cin.get(inChar); // Get the next character
      }
      cout << endl;
      return 0;
     }
      
  2. End-of-file controlled loops - This type of while loop is usually used when reading from a file. The loop terminates when the end of the file is detected. This can easily be determined by checking the EOF() function after each read from the file. This function will return true when the end-of-file is reached. In the sample code below integer values are read from a data file until the end of the file is reached.
     // EOF controlled loop
     #include<iostream>
     #include<fstream>
     using namespace std;
    
     int main()
     {
      ifstream   inData;
      int        intValue;
      inData.open("myData.txt");
      if(!inData.good()
      {
       cout << "Failed to open myData.txt. Program terminating.\n"; 
       return 0
      }
      while(!inData.EOF())
      {
       inData >> intValue;
       cout << "Got value: " << intValue << endl;
      }
      inData.close();
      return 0;
     }
      
  3. Flag controlled loops - A bool variable is defined and initialized to serve as a flag. The while loop continues until the flag variable value flips (true becomes false or false becomes true). In the following example the user is asked to enter numbers at the keyboard which are added together. To stop the addition the user enters a negative number.
     // Flag controlled loop
     #include<iostream>
     using namespace std;
    
     int main()
     {
      double sum = 0.0;
      double value;
      bool nonNegative = true; // Init flag
      while(nonNegative)  // Test if nonNegative is true
      {
       cin >> value;
       if(value < 0)
        nonNegative = false;
       else
        sum += value;
      }
      cout << "Sum of input numbers is " << sum << endl;
      return 0;
     }
      

No comments:

Post a Comment