1. For loop:
The syntax of for loop is for(initialization; checking ; update loop variable)
{
Body of loop;
}
First part, initialization is executed once, then checking is carried out if it is true the body of the for loop is executed. Then loop variable is updated and again checking is carried out this process continues until the checking becomes false. It is an entry controlled loop.
eg: for(i=1,j=1;i<=10;i++,j++)
cout<<i<<"x"<<j<<" = "<<i x j;
2. While loop:
It is also an entry controlled loop The syntax is given below
Loop variable initialised while(expression)
{
Body of the loop;
Update loop variable;
}
Here the loop variable must be initialised outside the while loop. Then the expression is evaluated if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.
eg:
i = 1;
j = 1;
while(i<=10)
{
cout<<i<<" x <<j<<" = "<<i x j;i++;
j++;
}
3. do While loop:
It is an exit controlled loop. The syntax is given below
do
{
Statements
}while(expression);
Here the body executes atleast once even if the condition is false. After executing the body it checks the expression if it false it quits the body otherwise the process will be continue.