All about while loop and do while loop in C Programming


 While loop

While loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body.

In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It is different in do while loop which we will see shortly.

The syntax,Flowchart and Example of while loop in c language is given below:


  while(condition)
   {
    code to be executed;
   }
 

Flowchart  :

Example :


 #include <stdio.h>
 int main()
 {
 int count=1;
 while (count <= 4)
 {
  printf("%d ", count);
  count++;
 }
 return 0;

  Output : 2 3 4 5

do while loop

 do while loop is a control flow statement that executes a block of code at least once, and then either repeatedly executes the block, or stops executing it, depending on a given boolean condition at the end of the block.

A do while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.

As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.

The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon.

Below is the Syntax,Flowchart and Example of do while loop

Syntax :

 do
 {
  code to be executed;
 }while(condition); 
 

Flow chart :


Example :

 #include <stdio.h>
 int main () 
 {
  int a = 10;
  do 
  {
   printf("value of a: %d \n",a);
   a = a + 1;
  }while( a < 15 );
  return 0;
 }
   Output :
   value of a: 10
   value of a: 11
   value of a: 12
   value of a: 13
   value of a: 14


Difference between while and do while loop

As we can see from the above flowchart the major difference between while and do while is that the while loop executes the body only if the condition is true whereas the do while loop executes the body atleast once even if the condition is false.

Post a Comment

0 Comments