Computer Programming Lab 7
Computer Programming Lab 7
Repetition Statements-II
1: Objective:
• To learn about syntax and implementation of for loops
2: For Loops
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
specific number of times. Syntax
for ( inital; condition; increment )
{ statement(s);
}
#include <iostream>
int main ()
a = a + 1 )
return 0;
Output
3: Nested Loops
A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.
Syntax
The syntax for a nested for loop statement in C++ is as follows −
while(condition)
{ while(condition)
{ statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested do...while loop statement in C++ is as follows −
do { statement(s); // you can put more
statements.
do {
statement(s); }
while( condition );
} while( condition );
Example Program
() {
return 0;
}
Output
Computer Programming Lab 7
3: Practice Exercise
Lab Rubrics