Understanding do...while loop in C

Understanding do...while loop in C

27 May 2024
Beginner
1.69K Views
5 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

do...while Loop in C: An Overview

The do-while loop is similar to the while loop. But this loop will execute the code block once, before checking if the condition is true. Afterward, this loop will repeat itself as long as the condition is true.

In this C Tutorial, we will explore more about the do...while loop which will include what is do...while in C, do...while loop in c programming with example, and the syntax of do...while loop in C. 

Read More - Top 50 C Interview Questions and Answers

Types of Loop in C

Let’s get into the three types of loops used in C programming.
  1. for loop
  2. while loop
  3. do while loop

But right now we are going to focus on only Do while Loop, So let's discuss it.

What is a do...while loop?

Do While is an exit-controlled loop. It prints the output at least once before checking the condition. Afterwards, the condition is checked and the execution of the loop begins.

Flowchart :

  

Syntax

do{
//code to be executed
}while(test condition);
  • The body of the loop executes before checking the condition.
  • If the test condition is true, the loop body executes again.
  • Again the test condition is evaluated.
  • The process repeats until the test condition becomes false.

Example: do...while loop in C

Let's elaborate do-while loop in C Compiler.

#include <stdio.h>
int main() 
{
 int i = 0;
 do {
 printf("%d\n", i+1);
 i++;
 }
 while (i < 10);
 return 0;
}
  • The above code prints numbers from 1 to 10 using the do while loop in C.
  • It prints 1 before checking if i less than 10.
  • It checks the condition i<10 and executes until i becomes 10

Output

1
2
3
4
5
6
7
8
9
10
Conclusion:
So we explored What is Do while in C, and Do While loop in C programming with example, the syntax of the Do while loop in C. Master your coding base in C programming to the next level by pursuing a C Programming Course, which will validate your expertise in loops and other important concepts.

FAQs

It is used to loop through a code until the given condition is satisfied.

Because,it executes the body of the loop and then validates the condition or test expression

  • The structure of a do-while loop is simple and easy to understand anyone.
  • The code inside a do-while loop is always executed at least once in life, regardless of the condition.
  • The code inside a do-while loop will continue to execute until the condition is True.

Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this