Wednesday 23 October 2013

For Loop

# include<iostream>
# include<conio.h>
void main()
{
int i;
for(i=0;i<9;i++)
cout<<"i am JD";

}
In this program,we will discuss how for loop works in c++......!
for(i=0;
what is this??
here we simply declare i to 0...
then we i<9
this is condition this for loops works when i is less then 9
and at last i++;
this is the increment operator we discuss in previous post...
first i will become zero then the condition will check if the condition is true then the loop prints whatever type in next line or compiler will do what is on next line.....!
Note:
if single line you want to execute with for loop then brackets cannot be needed,but if you make two lines or more then,then to execute with for loop then you will put brackets ....)
after doing what is in the brackets(if two or more lines are),then the loop go to the last one(i++),here i will be incremented and then i will become 1 then a condition will again check either it is less thhen 9 or not.if it is,then loop body will execute otherwise not...
Output is:-
i am JD
i am JD
i am JD
i am JD
i am JD
i am JD
i am JD
i am JD
i am JD
i am JD
Fell free to give suggestions in comment box