Loops
A for loop has the following syntax:
for(initial_value,condition,step){
// code to execute inside loop
};
Here is a for loop example:
int cNum=0;
for (int i=1; i < 8; i++){
cNum = cNum + i;
}
A while loop has the following form:
while(condition) {
// code to execute
};
Let's see an example of while loop:
int cNum = 2;
while (cNum< 12){
cerr << cNum<< endl;
cNum += 2;
}
The output of the preceding program would
be:
2
4
6
8
10 |