Mastering the do-while Loop in C: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-05 04:57:17   9774  Share
Subject Syllabus DetailsSubject Details 1 Questions 7 Program
☰ TContent
☰Fullscreen

Table of Content:

do-while loop

A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. do-while Statement is Exit Controlled Loop because condition is check at the last moment. In do-while loop body gets executed once whatever may be the condition but condition must be true if you need to execute body for second time.

 
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);

Braces { }:

The curly braces are unnecessary if only a single statement is being repeated.

Loop body:

In do-while loop body gets executed once whatever may be the condition but condition must be true if you need to execute body for second time. A one-time execution of a loop body is referred to as an iteration (or repetition) of the loop

loop-continuation-condition:

The condition can be any Boolean expression.

 

The loop body is executed first, and then the loop-continuation-condition is evaluated. If the evaluation is true, the loop body is executed again; if it is false, the do - while loop terminates.

flowchart of do-while loop

do while loop in c programming language

Example of do while loop:

 

#include<stdio.h>
void main()
{
        int n = 0;
	do {
		printf("Number %d \n",n);
		n++;
	   } while(n < 10);
} 

 

Output

 Number 0
Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
Press any key to continue . . .
 

Infinitive do-while Loop in C

If you pass true in the do-while loop, it will be infinitive do-while loop.

  do{  
//code to be executed  
}while(true);  

Example 1:

 
#include<stdio.h> 
void main()
{
   do{
     printf("infinitive do while loop\n");
     }while(1==1);
} 

 

Output

infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
........................
........................
........................
........................
........................
........................
upto infinite time it will print like this
 

Example 2:

 

#include<stdio.h> 
void main()
{
   do{
     printf("infinitive do while loop\n");
     }while(21 > 1);
} 

 

Output

infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
infinitive do while loop
........................
........................
........................
........................
........................
........................
upto infinite time it will print like this
 

Difference Between while and do-while Loop

Key Differences Between while and do-while Loop:

  1. The while loop checks the condition at the starting of the loop and if the condition is satisfied statement inside the loop, is executed. In the do-while loop, the condition is checked after the execution of all statements in the body of the loop.
  2. If the condition in a while loop is false not a single statement inside the loop is executed, and if the condition in ‘do-while’ loop is false then also the body of the loop is executed at least once then the condition is tested.
BASIS FOR COMPARISON WHILE DO-WHILE
General Form 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.

Point to Remember

Both while and do-while loop are the iteration statement, if we want that first, the condition should be verified, and then the statements inside the loop must execute then the while loop is used. If you want to test the termination condition at the end of the loop, then the do-while loop is used.

MCQ Available

There are 4 MCQs available for this topic.

4 MCQ