do-while loop in C programming with example

What is a loop?

Loops are used to execute the block of code several times according to the condition given in the loops. Every loop required initialization, condition, increment/decrement in the C programming language.

do-while loop in C

The do-while loop in C is also used to execute a statement or a set of statements repeatedly as long as the condition is true. It can be used when you have to enter the loop at least once. do-while loop tests its condition after it’s been executed at least once. Since the condition is tested at the bottom of a loop, its block of code is always executed at least once. It always executed at least once It is referred to as the exit control structure.

Syntax:

do
{
   body of the loop;
   action 1;
}
  while(condition test);
  action 2;

Flowchart of a do-while loop:

Flowchart of do-while loop

The do-while loop is used for looping statements. The do-while loop is similar to the while loop but the statement inside the do-while loop will be executed the first time however, the condition is true.

Example of a do-while loop:

#include<stdio.h>
#include<stdlib.h>
int main()
{
 int i=1;
 do {
  printf("value is: %d\n", i);
     i++;
 }
 while (i<=5);
 return 0;
}
 

Output:

value is: 1
value is: 2
value is: 3
value is: 4
value is: 5

Previous Lecture:

Data Types
Escape Character
Conditional statements-if-if-else

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the do-while loop in C lecture.

Leave a Comment

en English
X