🌙

Do while loop in C

The do-while loop is an exit-controlled loop, meaning it executes the code block at least once, even if the condition is false. The condition is checked after executing the loop body.

Flowchart:-

Syntax:

do{
    //code to be executed
}while(condition);
Example 1:
#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("%d\n", i);
        i++;
    } while (i <= 10);

    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10
Example 2: Multiplication Table using do-while loop in C
#include <stdio.h>

int main() {
    int num, i = 1;

    printf("Enter a number: ");
    scanf("%d", &num);

    do {
        printf("%d x %d = %d\n", num, i, num * i);
        i++;
    } while (i <= 10);

    return 0;
}

Output:

Enter a number: 5

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
Example 3: Star printing using do-while loop in C
#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("* \n", i);
        i++;
    } while (i <= 10);

    return 0;
}

Output:

* 
* 
* 
* 
* 
* 
* 
* 
* 
*