Loops in C++ Language, Part 2

category: 

This type of a loop keeps track of how many times the loop is executed.This loop is totally different from a count-controlled loop because the expression does not depend on the count. The count is used to keep track of the value read in. Cosider the program counting.cpp that deals with character data. It reads character by character until a period is reached. We assume that the input line contains a period before the end of line. The assumption is to terminate the loop before the end of the input file.genrally we do not make such an assumption but this is purely for the fundamental concept of how to keep track of the input data. The following program counting.cpp satisfies our explanation.

Program

  1. //program name: counting.cpp
  2. #include
  3. #include
  4. const PERIOD = ‘,’;
  5. int main ()
  6. {
  7. clrscr ();
  8. char ch;
  9. int count = 0;
  10. cout<<’’period is the terminator\n’’;
  11. while ((ch = cin.get ())! =PERIOD)
  12. {
  13. count++;
  14. cout<
  15. }
  16. cout<<’’\nTotal characters read excluding period: ‘’<
  17. return 0;
  18. }

Run output

Period is the terminator

Altafullahfarooq

Total characters read excluding period: 17

At the termination of the loop, count contains the number of characters written one less than the number read because period has been excluded. Assume there is no character in a line except a period. What would count contains?

Flag controlled loops

This is also known as the event-controlled loop. It is one whose execution is controlled by the occurrence of a specific value within the loop body. It is executed repeatedly executing the statement in the loop body as long as the variable is not changed. Suppose we want to set a flag to true before entering the loop and we wish to terminate the loop we set the variable to false. Consider the following program that would read and summing values until it encounters 999. In the program fcontrol.cpp, the process continues until 999 is read and the variable test becomes FALSE. When this happens, the body of the loop is notexecutedagain. After termination of whileloop, the value of variable sum is then sent to the output stream.

Program

  1. //program name: fcontrol.cpp
  2. #include
  3. #include    //used for setw ()
  4. #include
  5. const TRUE = 1, FALSE = 0, LASTUM =999;
  6. int main ()
  7. {
  8. clrscr ():
  9. int currval, test = TRUE, sum = 0;
  10. cout<<’’to exit, enter 999’’<
  11. while (test == TRUE)
  12. {
  13. cin>>currval;
  14. cout<
  15. if (currval! =LASTUM)
  16.       Sum+=currval;
  17. else
  18.       test = FALSE
  19. }
  20. cout<<’’\nSUM = ‘’<
  21. return 0;
  22. }

Run output

10 20 30 40 50 999

SUM = 150

If we do not set the flag (test) to FALSE it will never stop and will always go back to the iteration head and starts executing the statement again and again. Variable sum is used as an accumulator and must be initialized either at the time of declaration or before the loop. The statement sum+=currval is used not to decrease the execution time but to reduce the typing code.

Bad data using while loop

When we create an input file, at some point we close it; the system puts the end of file marker at this point. Reading a character, we always test the EOF marker whether we reached to the end of file. The end of file becomes true when the next character to be read is the EOF (end-of-file) character. Different systems provide different ways of indicating an end of file when the system is reading from the keyboard.pressctrl+Z(control plus z) and then the enter, n works for most of the systems. Some systems use ctrl+D and then enter and some systems use ctrl+D and then ctrl+Z.this varies from system to system.the use of bad data is shown in program cof.cpp.

Program

  1. //program name: cof.cpp
  2. #include
  3. #include
  4. int main()
  5. {
  6. clrscr();
  7. int num, count = 0, sum = 0;
  8. cout<<’’to exit,entercontrol+Z. otherwise any integer value’’<
  9.  
  10. while (cin>>num)   //assuming 100, 200, 300
  11. {
  12. sum  +=num;
  13. count++;
  14. cout<<’’to exit, enter control+Z.otherwise any integer value’’<
  15. }
  16. if (count > 0)
  17. cout<<’’there were ‘’<                 
  18. else
  19. cout<<’’no entry’’<
  20. return 0;
  21. }

Run output

To exit, enter control+Z, Otherwise any integer value

There were 3 number and the SUM= 600

To exit, enter control+Z, Otherwise any integer value

No entry

Always test the end-of-file condition before processing the data and use a priming input statement before performing the loop. Another important step should not be forgotten that is to use input at the bottom of the loop.the solution of while loop is suitable than some other loops because the exact number of input is not known.

The while loop guidelines