Do…While Loop In C

Do…While Loop In C

Loops in C are control flow constructs that enable the repetition of a specific block of code until a particular condition is met. The do-while loop, along with the while loop and for loop, is one of the three primary loop constructs in C.

The do-while loop is particularly useful when you need to execute a block of code at least once, even if the condition is not initially met. It is commonly used for tasks like traversing arrays, vectors, and other data structures where you want to ensure that the loop body runs at least once before checking the loop condition.

What is do…while Loop in C?

The do…while in C is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after executing the body of the loop. Due to this, the statements in the do…while loop will always be executed at least once no matter what the condition is.

Syntax of do…while Loop in C

do {
 
    // body of do-while loop    
    
} while (condition);

How to Use do…while Loop in C

The example below shows the do-while Loop in C Program

// C Program to demonstrate the use of do...while loop
#include <stdio.h>
 
int main()
{
 
    // loop variable declaration and initialization
    int i = 0;
    // do while loop
    do {
        printf("Skill Vertex\n");
        i++;
    } while (i < 3);
 
    return 0;
}

Output

Skill Vertex 
Skill Vertex 
Skill Vertex

The do-while loop in C is unique because it always runs its code block at least once. Here’s how it works:

  1. First, it executes the code inside the loop.
  2. Then, it checks a condition.
  3. If the condition is true, it repeats the process.
  4. If the condition is false, it moves on to the next part of the program.

This loop is sometimes called an “exit-controlled” loop because it checks the exit condition after running the code block. It’s handy when you want to ensure that certain code runs at least once before checking if it should repeat.

Nested do…while Loop in C

// C Program to demonstrate the nesting of do...while loop
#include <stdio.h>
 
int main()
{
 
    // declaring loop variables
    int i = 0, j;
    int count = 0;
 
    // outer loop starts
    do {
        j = 0;
 
        // inner loop starts
        do {
            printf("%d  ", count++);
            j++;
        } while (j < 3);
        // inner loop ends
 
        printf("\n");
        i++;
    } while (i < 3);
    // outer loop ends
 
    return 0;
}

Output

0  1  2  
3  4  5  
6  7  8  

Examples of Do…While Loop in C

Example 1. C Program to check the behavior of do…while loop if the condition is false from the start.

// C Program to demonstrate the do...while loop behaviour
// when the condition is false from the start
#include <stdbool.h>
#include <stdio.h>
 
int main()
{
 
    // declaring a false variable
    bool condition = false;
 
    do {
        printf("This is loop body.");
    } while (condition); // false condition
 
    return 0;
}

Output

This is loop body.

The do…while loop operates in C, states the fact that the loop body is executed at least once, even when the condition is initially false. This is due to the specific behavior of the do…while loop, where the condition is checked after the first execution of the loop body.

Example 2. C Program to print Multiplication Table of N using do…while loop

// C Program to print multiplication table using do...while
// loop
#include <stdio.h>
 
int main()
{
 
    int N = 5, i = 1;
 
    do {
        printf("%d\tx\t%d\t=\t%d\n", N, i, N * i);
    } while (i++ < 10);
 
    return 0;
}

Output

5    x    1    =    5
5    x    2    =    10
5    x    3    =    15
5    x    4    =    20
5    x    5    =    25
5    x    6    =    30
5    x    7    =    35
5    x    8    =    40
5    x    9    =    45
5    x    10    =    50

Key Difference between while and do…while Loop in C

while Loopdo…while Loop
The test condition will be checked before the loop body is executed.The test condition will be checked after executing the body.
When the condition is false, the body won’t be executed not even once.The body of the Do…while loop will be executed at least once even though the condition turns out to be false.
Do … While Loop is a pre-tested or entry-controlled loop Do … While Loop is a pre-tested or entry-controlled loop
Semicolon is not required.A semicolon is not necessary.

FAQ- Do…While Loop In C

Q1. What does a do-while loop do in C?

Ans. The do-while statement allows you to repeatedly execute a statement or a block of statements until a specified expression evaluates to false.

Q2. What is the do while looping statement?

Ans. A do-while loop is indeed a control flow statement that guarantees the execution of a block of code at least once. After the initial execution, it repeatedly re-executes the block based on a specified boolean condition at the end of the block.

Q3. When to use while and do-while loop in C?

Ans. In a “while” loop, the condition is checked first, and if it is true, the statement(s) are executed. If the condition is false initially, the statement(s) may not execute at all.
In a “do-while” loop, the statement(s) are executed at least once, regardless of the initial condition. After the first execution, the condition is checked, and if it is true, the loop continues to execute.


Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment