C Do-While

Learn the 'do-while' tutorial right now.

Do-While Loop:

A 'do-while' loop is a control structure in the C programming language that allows you to repeatedly execute a block of code as long as a certain condition is true. Unlike the 'while' loop, which tests the condition before entering the loop, the 'do-while' loop tests the condition after the loop has executed at least once. This guarantees that the loop will always run at least once, regardless of whether the condition is initially true or false.

do {
    // Code to be executed
} while (condition);

Here's how the 'do-while' loop works:

1. The code block inside the 'do' braces is executed first.

2. After the code block has been executed, the condition inside the 'while' parentheses is evaluated.

3. If the condition is true, the loop will continue to execute, and the code block will be executed again. If the condition is false, the loop will terminate, and program control will move to the next statement after the 'do-while' loop.

Here's an example of a simple 'do-while' loop that counts from 1 to 10:

c Copy Code
#include <stdio.h>

int main() {
    int i = 1;

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

    return 0;
}

Explanation:

The loop will always execute at least once because the condition 'i <= 10' is true when 'i' is initially set to 1. The loop will continue to run as long as the condition remains true, and it will terminate when 'i' becomes greater than 10.

Output:
1 2 3 4 5 6 7 8 9 10

What is New?

We have just updated this website & still working on adding new content & updating existing features.