Examples of do…while Loop in C

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

C




// 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.

As we can see, even when the condition is false at the start, the loop body is executed once. This is because in the do…while loop, the condition is checked after going through the body so when the control is at the start,

  1. It goes through the loop body.
  2. Executes all the statements in the body.
  3. Checks the condition which turns out to be false.
  4. Then exits the loop.

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

The following example demonstrates the use of do…while loop for printing the multiplication table of N.

C




// 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

do…while Loop in C

Loops in C language are the control flow statements that are used to repeat some part of the code till the given condition is satisfied. The do-while loop is one of the three loop statements in C, the others being while loop and for loop. It is mainly used to traverse arrays, vectors, and other data structures.

Similar Reads

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 following example demonstrates the use of do…while loop in C programming language....

How does the do…while Loop works?

...

C do…while Loop Flowchart

Syntax Structure of do while loop...

Nested do…while Loop in C

Flowchart of do…while Loop in C...

Examples of do…while Loop in C

As with other loops, we can also nest one do…while loop into another loop. It is demonstrated using the following C program....

Difference between while and do…while Loop in C

...

Conclusion

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

FAQs on C do…while Loops

...