Posted By:
2 Jul, 2013 6:15 am PDT

Implementation of for loop in C++

What is a for loop? How to implement them?

The use of the for loop is same as that of the while loop and it also behaves in the same manner. But the initialization, condition and increment/decrement operation is written in the same line in case of ‘for’ loop.

Syntax of for loop is:


for (initialization; condition; increment/decrement)







//To find the sum of ‘n’ numbers


void main()


int n,sum=0;

for(int i=0;i<n;i++)


sum=sum + i;





The initialization and increment fields are optional, but the semicolon signs between them should always be present. Optionally, using the comma (,) operator we can specify more than one expression in any field we want.


Was it useful?  

Please login to reply to this problem.