2. (5pts.) What is a Count controlled loop? C++
Count-controlled loops use a
counter (also referred to as loop
index) which countsspecific items or values and
causes the execution of the loop to terminate when
the counter has incremented or decremented a set
number of times. Event-Controlled loops use an
event to control the iteration of the
loop.
LOOPING IN C++
The while statement is like the if statement in that it tests a condition. just like an if statement, the condition in a while statement can be an expression of any simple data type. In C++, any nonzero value is coerced to type Boolean true, and zero is coerced to type Boolean false. If the expression evaluates to false, then execution passes beyond the while loop; if it evaluates to true, than the code in the while loop executes once and then the conditional expression is once again evaluated. Each pass through the loop is called an iteration, and before each iteration there is a loop test.
The statement or group of statements to be executed each time through the loop is known as the body of the loop. The body of the loop must be enclosed in curly braces if contains more than one statement.
The condition that causes the loop to exit is the termination condition. There are two different types of loops, depending on what constitutes the termination condition. The first type of loop is the count-controlled loop, which is a loop that executes a specified number of times. The second type of loop is the event-controlled loop, which terminates when something has occurred inside the loop body. This sort of loop is used when working with variable data, such as user input, or searching the contents of a file.
A count controlled loop uses a variable called the counter or iterator in the loop test. Before we start the loop, we must initialize the counter variable, and in each iteration of the loop we must modify the counter in some way so that it reaches a termination condition.
#include <iostream> using namespace std; int main(void){ int counter = 1; while(counter <= 10){ cout << "In the loop!" << endl; cout << "Incrementing the counter variable..." << endl; counter++; cout << "End of the loop body!" << endl; } return 0; }
Get Answers For Free
Most questions answered within 1 hours.