A Sentinel Loop Asks the User Whether to Continue on Each Iteration
In the while loop we have to be careful to properly initialize the condition being testing in the loop. If that condition is false at the start then nothing inside the loop is ever executed. Sometimes we want to make sure we execute the loop at least once. The do-while loop is just right for this. In a do-while loop the condition is not tested until the end of an iteration through the loop, therefore it will always execute at least one. Notice it is in essence a while loop turned upside down. Here is a simple example of a do-while loop:
The for Loop |
Basic layout of the for loop:
The while and do-while loops are meant to be used when you do not know how many times you will need to repeat an action. Some condition will change in the loop to terminate it. If you do know exactly how many iterations you will be making then the for loop is the better choice. There are three parts inside the parentheses of a for loop statement. The first is where the loop counter variable is initialized to a starting value. You can declare and initialize the variable here or just use a previously declared integer variable. The second part of the for statement is the test which determines when the loop terminates. The third part is the increment. This can be a simple increment by 1 (loopCount++ or loopCount += 1 or loopCount = loopCount +1), or an increment by any other amount (loopCount += 2 or loopCount = loopCount + 2). You can also initialize the loop counter variable to a larger number, test while the loop counter is greater than zero, and then use a decrement of the loop counter value [for(int i=10; i>0; i--)]. BTW there is a long tradition of using variables named i, j, or k as loop counter variables because these were reserved variable names in the Fortran programming language back in the 1950s and 1960s.
Below is a sample of a for loop. Note that this is the same count-controlled loop that is shown above using both a while and a do-while loop. In this example the loop counter variable, loopCount, is declared inside the for loop statement. Once the for loop exits this variable will ceast to exist. If you want to use the variable again you will need to declare it above the for statement and then only initialize it (loopCount=1) in the for statement.
// Count controlled loop #include<iostream> using namespace std; int main() { for(int loopCount=1; loopCount<=10; loopCount++) { cout << "Hello! Loop count = " << loopCount << endl; loopCount++; // Increment control variable } return 0; }
Designing a loop
It take programming skill to design a good loop. There are sevral questions you should answer to guide you in this process.Questions which should be asked when designing any loop structure
- What is the condition that ends the loop? In most cases this is stright forward, but make sure you know exactly what that condition is and make sure it can occur.
- How should the condition be initialized? Make sure the loop control variable is appropriately initialized so the loop doesn't terminate immediate before doing anything. If you fail to properly initialize the pre-conditions before starting a loop the results can be undefined.
- How should the condition be updated? Is this taken care of automatically by comparing input values to a sentinel value, or do you have to increment a counter, check a flag, etc.
- What is the process being repeated? What action is coded inside the loop to be repeated?
- How should the process be initialized? Are there any variables, etc. that need to be initialized in addition to the loop control variable before starting the loop? For example, if summing a list of values you need to initialize sum to zero. This is not done automatically when you create the variable.
- How should the process be updated? What processes are executed inside the loop? For example, in summing a list of values: (1) get the next value, (2) add it to the running sum.
- What is the state of the program on exiting the loop? If there anything you need to do to clean up, like closing files that were used in the loop, or outputting values calculated in the loop?
Source: https://www.cs.uah.edu/~rcoleman/CS121/ClassTopics/Loops.html
0 Response to "A Sentinel Loop Asks the User Whether to Continue on Each Iteration"
Postar um comentário