Various forms of while loop | Control Structures
Various forms of while loop:
Case 1: Counter-Controlled while Loops:
Suppose you know exactly how many times certain statements need to be executed. For example, suppose you know exactly how many pieces of data (or entries) need to be read. In such cases, the while loop assumes the form of a counter-controlled while loop. That is, the LCV serves as a “counter.” Suppose that a set of statements needs to be executed N times. You can set up a counter (initialized to 0 before the while statement) to track how many items have been read. Before executing the body of the while statement, the counter is compared with N. If counter < N, the body of the while statement executes. The body of the loop continues to execute until the value of counter >= N. Thus, inside the body of the while statement, the value of counter increments by 1 after it reads a new item. In this case, the while loop might look like the following:
counter 5 0; //initialize the loop control variable
while (counter < N) //test the loop control variable
{
.
.
.
counter++; //update the loop control variable
.
.
.
}
Case 2: Sentinel-Controlled while Loops:
You do not always know how many pieces of data (or entries) need to be read, but you may know that the last entry is a special value, called a sentinel, that will tell the loop to stop. In this case, you must read the first item before the while statement so the test expression will have a valid value to test. If this item does not equal the sentinel, the body of the while statement executes. The while loop continues to execute as long as the program has not read the sentinel. Such a while loop is called a sentinel-controlled while loop. In this case, a while loop might look like the following:
cin >> variable; //initialize the loop control variable
while (variable !5 sentinel) //test the loop control variable
{
.
.
.
cin >> variable; //update the loop control variable
.
.
.
}
Case 3: Flag-Controlled while Loops:
A flag-controlled while loop uses a bool variable to control the loop. A flag variable is a bool variable that indicates whether a condition is true or false. It is generallynamed for the true state of that condition: for example, isFound, isTallEnough, and isFull. Suppose isFound is a bool variable. The flag-controlled while loop takes the following form:
isFound = false; //initialize the loop control variable
while (!isFound) //test the loop control variable
{
.
.
.
if (expression)
isFound = true; //update the loop control variable
.
.
.
}
Case 4: EOF-Controlled while Loops:
If the data file is frequently altered (for example, if data is frequently added or deleted), it’s best not to read the data with a sentinel value. Someone might accidentally erase the sentinel value or add data past the sentinel, especially if the programmer and the data entry person are different people. Also, it can be difficult at times to select a good sentinel value. In such situations, you can use an end-of-file (EOF)-controlled while loop.
Until now, we have used an input stream variable, such as cin, and the extraction operator, >>, to read and store data into variables. However, the input stream variable can also return a value after reading data, as follows:
1. If the program has reached the end of the input data, the input stream variable returns the logical value false.
2. If the program reads any faulty data (such as a char value into an int variable), the input stream enters the fail state. Once a stream enters the fail state, any further I/O operations using that stream are considered to be null operations; that is, they have no effect. Unfortunately, the computer does not halt the program or give any error messages. It just continues executing the program, silently ignoring each additional attempt to use that stream. In this case, the input stream variable returns the value false.
3. In cases other than (1) and (2), the input stream variable returns the logical value true.
You can use the value returned by the input stream variable to determine whether the program has reached the end of the input data. Because the input stream variable returns the logical value true or false, in a while loop, it can be considered a logical expression. The following is an example of an EOF-controlled while loop:
cin >> variable; //initialize the loop control variable
while (cin) //test the loop control variable
{
.
.
.
cin >> variable; //update the loop control variable
.
.
.
}
Notice that here, the variable cin acts as the loop control variable.
Comments
Post a Comment