In this tutorial section we will learn Difference between while loop and do while loop in C Language .
Both of while and do while loops are types of iterative/looping statements used in C language, but there is a some difference between these in C. In this tutorials, we will learn the difference between while and do while loop in a tabular form. But let us first know a bit more about each of these.
What is a while loop?
The while loop in C programming is refers to a control flow statement or iterative statements that basically execute block of a code or statements on the basis of any given Boolean condition. Therefore, we can say that a while loop as a type of a iterative or repeating statement.
What is a do-while loop?
do-while loop is known as type of Exit Control Loop. The do...while loop is quite similar to a while loop, except the fact that it is execute at least once.
The do-while loop is almost similar to that of the while loop. But the only difference is that this loop checks for the conditions after execution of statements in the body of loop.
Difference between while loop and do while loop in C Language
Following are the list of the differences between while and do-while loop in C programming
While Loop:
1. In While loop first needs to check the condition, and only then we can execute the statement(s).
2. while(condition)
No semicolon is present at the end of the while loop.
3. We don’t require any brackets if there is only a single statement.
4. We always need to initialize a variable used in a condition before the execution of this loop.
5.The while loop is an entry-controlled type of loop.
Do While Loop:
1.The execution of statement(s) occurs at least once. After that, the checking of the condition occurs.
2. while(condition);
A semicolon is present at the end of the do-while loop.
3. We would always require brackets in this case.
4.The initialization of the variable may occur within or before the execution of the loop.
5.The do-while loop is an exit-controlled type of loop.
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:
Variables and Identifiers Relational Operators if-else statements Switch case While Loop Infinite while Loops C FOR Loop Infinite for Loops Continue in Loops One Dimensional Array Two Dimensional Arrays Read and Display 2D Arrays Types of functions Passing Array To Functions Nesting of Function Array vs Structure Array of Structure Structures and Functions Structures Within Structures Use Of Pointers In C File Handling In C Loops FAQ Arrays FAQ count vowels in a file Function FAQ Conditional Statements Assignments For Loops Assignments Arrays Assignments Function Assignments Structure Assignments Pointers Assignments Files Assignments Storage classes Assignments Binary Files count words,lines in a file Copy files Update File Continue in Loops break in Loops Difference Between While and Do while difference while do..while & for malloc calloc Storage Classes Operators MCQ Conditional Statements MCQ Loops MCQ Arrays MCQ Function MCQ Structure MCQ Pointers MCQ Files MCQ Storage classes MCQ