difference between while and do while loop in c programming

Given Diagram is for the difference between while and do while loop in c programming.
let us understand difference between while and do while loop on by one.

1.General form(Syntax): The General form or Syntax of
i. while loop is
while(condition)
{
body of loop //statements
}
and
ii. do while loop is
do
{
body of loop // statements
}while(condition);
2.Controling Condition:
I. In while loop the condition is tested before the body of the loop or at the top of the loop. The statements or body of the loop executes till condition is true.
II. In do while loop the condition is tested after the body of the loop or at end of the loop. The statements or body of the loop executes at least once even though condition is false.
3.Iteration: Iteration in c programming defined as the fundamental concept that repeates the set of statements or instruction multiple times until a certain condition is true.
i. While loop: if the condition mentioned in the while loop is false then the body of the loop not execute or do not iterate through it otherwise executes commands till the condition is true or met.
ii. do while loop: The condition mentioned in the do while loop is false then the body of the loop executes at least once, otherwise executes commands till the condition is true or met.
4.Flow: : Flow diagram shows the pictorial representation of the while and do while loop.It show the direction or flow of the control in the loops.


C Program to illustrate use of while loop:

/* C program given below displays series of number 1 ,2,3.....10 */
#include<stdio.h>
int main()
{
 int i=1;
 while(i<=10)
  {
   printf("\n%d",i);
   i++;
  }
 return(0);
}
Output: 1 2 3 4 5 6 7 8 9 10
Explanation : Variable i is declared as int data type and initialized to 1 i.e int i=1
after that program control jumps to while and test the condition i<=10 i.e 1<=10 which is true and flow enters in to the loop and execute printf("\n%d",i); statement and shows output 1.
after that i++ executes which increase the value of i by 1. at this palce i becomes 2 i.e. i=2.
the control goes back to while and test the condition while(i<=10) i.e. 2<=10 which is again true then control enters in to the loop body and start executing the statements.
This process will continue while value of i is less than or equal to 10.
Once the value of i becomes 11 the loop will terminate and control jumps out of the while loop.


C program to illustrate use of do while loop

/* C program given below displays series of number 1 ,2,3.....10 */
#include <stdio.h>
int main()
{
 int i=1;
 do
 {
  printf("\n %d",i);
  i += 1;
 } while(i<=10);
}

Output: 1 2 3 4 5 6 7 8 9 10
Explanation : Variable i is declared as int data type and initialized to 1 i.e int i=1
after that program control start executing do while loop body and execute printf("\n%d",i); statement and shows output 1.
after that i+=1 executes which increase the value of i by 1. at this palce i becomes 2 i.e. i=2.
the control reach to while and test the condition while(i<=10) i.e. 2<=10 which is true then control enters in to the do part (loop body and)start executing the statements.
This process will continue while value of i is less than or equal to 10.
Once the value of i becomes 11 the loop will terminate and control jumps out of the while loop.
Note: do while loop executes its body at least once even the test condition given is false.

Previous Topic:-->> Nested while loop in C || Next topic:-->>Difference Between goto and loop in C.
Other Topics: