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

Implementation of for loop in C++

What is a for loop? How to implement them?
Attachment:None


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)

{

statement;

}

 

 

Eg:

//To find the sum of ‘n’ numbers

#include<iostream.h>

void main()

{

int n,sum=0;

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

{

sum=sum + i;

}

cout<<sum;

}

 

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.



Related Problems on Fixoncloud