category:
Table of Contents
-
- 0.1.Program
- 0.2.Run output
- 1.Flag controlled loops
- 1.1.Program
- 1.2.Run output
- 2.Bad data using while loop
- 2.1.Program
- 2.2.Run output
- 3.The while loop guidelines
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
- //program name: counting.cpp
- #include
- #include
- const PERIOD = ‘,’;
- int main ()
- {
- clrscr ();
- char ch;
- int count = 0;
- cout<
- while ((ch = cin.get ())! =PERIOD)
- {
- count++;
- cout<
- }
- cout<
- return 0;
- }
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
- //program name: fcontrol.cpp
- #include
- #include
//used for setw () - #include
- const TRUE = 1, FALSE = 0, LASTUM =999;
- int main ()
- {
- clrscr ():
- int currval, test = TRUE, sum = 0;
- cout<
- while (test == TRUE)
- {
- cin>>currval;
- cout<
- if (currval! =LASTUM)
- Sum+=currval;
- else
- test = FALSE
- }
- cout<
- return 0;
- }
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
- //program name: cof.cpp
- #include
- #include
- int main()
- {
- clrscr();
- int num, count = 0, sum = 0;
- cout<
- while (cin>>num) //assuming 100, 200, 300
- {
- sum +=num;
- count++;
- cout<
- }
- if (count > 0)
- cout<
- else
- cout<
- return 0;
- }
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