Beruflich Dokumente
Kultur Dokumente
Loop types:
Loop Type
while loop
Description
While a given expression is true it repeats the statement in the loop body. Before executing
the loop body, it tests the condition for true or false.
dowhile loop
It is like a while loop but it tests the condition after executing the loop body.
In above two loops we need to write the increment or decrement operation to break the loop
for loop
after sometime. But in for loop we have an option of incrementing or decrementing outside
for-each loop
nested loops
When using one or more loops inside a loop is known as nested loop.
Description
Break terminates immediately the loop statement from executing further and execution
reaches just outside the loop body containing the break statement.
Continue statement is equivalent to going to the very end of the loop immediately by
skipping further statements.
It is equivalent to skipping the further statements and immediately jumping to the
labeled statement.
Example:
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
The DOWHILE loop
The do while loop is almost the same as the while loop. It behaves like a while-loop, except
that condition is evaluated after the execution of statement instead of before, guaranteeing at least
one execution of statement, even if condition is never fulfilled.
The Dowhile uses the following structure:
do
{
do something;
}
while (expression);
Example:
cin >> howmuch;
counter = 0;
do
{
counter++;
cout << counter << '\n';
}
while ( counter < howmuch);