banner

For Loop

     
    Perhaps one reason why few programmers use while is that they are too busy using the for, which is probably the most popular looping instruction. The for allows us to specify three things about a loop in a single line:

(a) Setting a loop counter to an initial value.

(b) Testing the loop counter to determine whether its value has reached the number of repetitions desired.

(c) Increasing the value of loop counter each time the program segment within the loop has been executed.

for ( initialise counter ; test counter ; increment counter )
{
do this ;
and this ;
and this ;
}

example:-

for ( i=0; i<5; i++)
{
cout<<"Hello\n";
}
     Here the out put of this example will be
Hello
Hello
Hello
Hello
Hello

     Here i start with 0 and goes to 4 because 4 is less then 5 but when i =5 then the loop break, reason condition is false.    

Nestinf of Loops


for(j=0;j<3;j++)
{
for ( i=0; i<2; i++)
{
cout<<"Hello\t";
}
cout<<"\n";
}

Output:-

Hello          Hello
Hello          Hello
Hello          Hello