Thursday, September 3, 2015

The For loop in C++

Syntax: 
    for (initialization; condition; increase) 
         statement;
How does it works:
1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.
2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).
3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.
4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
Example:
#include<iostream>
using namespace std;
int main(){
int i;
for(i=0;i<10;i++)
cout<<i<<" ";    //statement;
return 0; 
}
Result:
for loop example

No comments:

Post a Comment