Understanding Do while loop in c

12 apr. 2024
Beginner
101 Views
5 min read  

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, then this loop will repeat the loop 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

Q1. What is a do-while loop?

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

Q2. Why is it called a do-while loop?

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

Q3. What are the advantages of do while loop?

  • 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