What determines the number of times that a condition-controlled loop will repeat
Which type you use will be determined by what you are trying to do, the conditions of the loop, and what will determine when the loop should terminate. The three types of loops in C/C++ are while, do-while, and for. Each one is covered below. Show
Basic layout of the while loop: The conditional expression used here is the same type of conditional expression used in the if command. An if statement is used to choose a course of action. A while statement is used to repeat some action as long as the conditional expression is true. Terms Related to LoopsThe Count-Controlled while loopIn 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 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 loopIn 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.
Designing a loopIt 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
Nested loopsYou can, and quite frequently will need to, nest loops inside of other loops. Here is a simple example:// Nested loops #include using namespace std; int main() { int limit1 = 3; // Set number of times to execute outer loop int limit2 = 5; // Set number of times to execute inner loop int outCount = 0; // Create and init outer loop counter int inCount; // Create inner loop counter while(outCount < limit1) { inCount = 0; // Init inner loop counter while(inCount < limit2) { cout << "outCount = " << outCount << " inCount = " << inCount << endl; } } return 0; } Basic layout of the do-while loop: 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: // Count controlled loop #include using namespace std; int main() { int loopCount; // loop control variable loopCount = 1; do { cout << "Hello! Loop count = " << loopCount << endl; loopCount++; // Increment control variable } while(loopCount <= 10); return 0; } 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. What kind of loop repeats a certain number of times?A loop that repeats a specific number of times is known as a count-controlled loop.
What controlled loop uses a true/false condition to control the number of times that it repeats?A condition -controlled loop uses a true/false condition to control the number of times that it repeats. A count -controlled loop repeats a specific number of times. Each repetition of a loop is known as a(n) iteration.
What is condition controlled loop?Condition Controlled Loops. ∎ A condition controlled loop is programming structure that. causes a statement or set of statements to repeat as long as a. condition evaluates to True.
What is a condition controlled loop quizlet?condition-controlled loop. A loop that uses true/false condition to control the number of times it repeats.
|