Professional Documents
Culture Documents
ELEMENTS OF LOOPING
1. Loop Control Variable (LCV)
The loop control variable will hold the value that will be compared to the limit of the
loop.
2. Sentinel Value
The sentinel value is the value where the loop control variable will be compared to.
3. Loop update
Inside the loop, the value of the loop control variable must be altered. This is called
the loop update.
1
CC102 C++ Loop 1
TYPE OF LOOPS
- for LOOP
- while LOOP
- do while LOOP
- nested LOOP
SYNTAX:
for (initialization; comparison expression; update)
{
statement(s);
}
while LOOP
Basically, it will do the repetitive tasks assigned to it just like in a for loop.
SYNTAX:
initialization;
while (comparison expression)
{
statement/s;
loop update;
}
do while LOOP
The only difference with for and while loops is that the comparison happens at the end of the do while loop so the
body of the loop is executed at least once even if the comparison expression evaluates to false, unlike in the for
loop and the while loop where the comparison expression happens before the loop body.
SYNTAX:
initialization;
do
{
statement/s;
loop update;
} while (comparison expression);
2
CC102 C++ Loop 1
NESTED LOOPS
A nested loop is a loop within a loop. It is consisting
of an outer loop and an inner loop.
EXAMPLE:
Break
Break statement is used to jump out of a loop.
Continue
The continue statement breaks one iteration, if a specified condition occurs, and continues with the next iteration
in the loop.