Pages

Saturday, 28 January 2017

Difference between while and do while

NIELIT O Level January 2016 Solve paper

Q 7 b.(ii) while and do..while loops

Iteration statements allow the set of instructions to execute repeatedly till the condition doesn’t turn out false. The Iteration statements in C for while loop and do while loop. These statements are commonly called loops. Here, the main difference between a while loop and do while loop is that while loop check condition before iteration of the loop, whereas do-while loop, checks the condition after the execution of the statements inside the loop.

BASIS FOR COMPARISON
WHILE
DO-WHILE
Syntax
while ( condition) {
statements; //body of loop
}
do{
.
statements; // body of loop.
.
} while( Condition );
Controlling Condition
In 'while' loop the controlling condition appears at the start of the loop.
In 'do-while' loop the controlling condition appears at the end of the loop.
Iterations
The iterations do not occur if, the condition at the first iteration, appears false.
The iteration occurs at least once even if the condition is false at the first iteration.

Definition


The while loop is the most fundamental loop available in C.  The general form of while loop is:
while ( condition)
 {
        statements; //run when condition is true
}
The while loop first verifies the condition, and if the condition is true then, it iterates the loop till the condition turns out false. The condition in while loop can be any boolean expression. When expression returns any non-zero or positive value, then the condition is “true”, and if an expression returns a zero or negative value, the condition becomes “false”. If the condition becomes true, then loop iterates itself, and if the condition becomes false, then the control passes to the next line of the code immediately followed by the loop.
Following code print number between 1 to 10
void main()
{
     int i;
     i=1;
     while(i<=10)
     {
           printf("\t%d",i);
           i++;
     }
}
Definition of do-while Loop
As in while loop, if the controlling condition becomes at entry point of loop, then the body of the while loop is not executed at all. But the do-while loop is somewhat different from while loop. The do-while loop executes the body of the loop at least once even if the condition is false at the first attempt.
The general form of do-while is as follows.
Do
{
.
statements; // Run at least ones if condition is false.
.
} while( Condition );
Example : Print number between 1 to 10
void main()
{
     int i;
     i=1;
     do
     {
           printf("\t%d",i);
           i++;
     }while(i<=10);
}
Main Difference between while and do-while loop
While
Do-while
While loop is known as entry control loop because condition check before entering to loop body.

void main()
{
     int i;
     i=1;
     while(i>10)
     {
           printf("\t%d",i);
           i++;
     }
}

No output will be generated because condition is check before entering of loop body and in this example condition is false because I is less than 10. That is why while loop is called entry control loop .
Do-while is known as exit control loop because condition is check to end of loop.

void main()
{
     int i;
     i=1;
     do
     {
           printf("\t%d",i);
           i++;
     }while(i>10);
}

Print 1 but condition is false before entering the loop body. Even then loop ran one time because condition is checked at end of the loop. This is the main reason why do-while loop called exit control loop and guarantee to run at least one time when condition is false.


Next Question

No comments:

Post a Comment