Header Ads Widget

Do-While Loop in C

Do-While Loop in C: Executing Code with Post-Condition Control

Understanding the Syntax and Functionality of the Do-While Loop in C Programming


In C programming, the do-while loop provides a powerful mechanism for executing a block of code at least once, and then repeatedly as long as a specific condition is true. Unlike the while loop, which checks the condition before the code execution, the do-while loop checks the condition after the code execution. This article will delve into the syntax and functionality of the do-while loop in C, provide examples to illustrate its usage, and discuss its advantages in various programming scenarios.

Syntax of the Do-While Loop:

The syntax of the do-while loop in C is as follows:

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

The loop executes the code block first and then checks the `condition`. If the `condition` is true, the loop continues executing the code block. If the `condition` is false, the loop terminates.

Example Code:

#include int main() {    int i = 1;    do {        printf("%d ", i);        i++;    } while (i <= 5);    return 0;}

1 2 3 4 5

In the above example, we initialize `i` to 1 and set the condition as `i <= 5`. The code block within the do-while loop is executed at least once, regardless of the condition. Inside the loop, we print the value of `i` and increment it by 1. The loop continues until `i` becomes 6, at which point the condition becomes false, and the loop terminates.

Advantages of the Do-While Loop:

1. Guaranteed Execution: The do-while loop guarantees the execution of the code block at least once, irrespective of the condition. This is useful in scenarios where the code block must be executed before the condition is checked.

2. Flexibility: The do-while loop offers flexibility in controlling the flow of the program. It can be used when the number of iterations is unknown beforehand or when a specific condition needs to be checked after executing the code block.

3. Simplified Input Validation: The do-while loop is commonly used for input validation, as it ensures that the code block is executed at least once, allowing the user to provide input before the condition is evaluated.

Example Code:

#include int main() {    int password;    do {        printf("Enter the password: ");        scanf("%d", &password);    } while (password != 1234);    printf("Access granted!");    return 0;}

In this example, the program prompts the user to enter a password. The do-while loop continues until the user enters the correct password (1234). Inside the loop, the program asks for the password and reads the input. Once the correct password is entered, the loop terminates, and the program displays “Access granted!”.


The do-while loop is a valuable iteration construct in C programming that allows developers to execute a block of code at least once and then repeatedly as long as a specific condition is true. With its post-condition control, it offers flexibility in program flow and ensures the execution of the code block. By understanding the syntax and advantages of the do-while loop, programmers can utilize its capabilities to write efficient and reliable code.


Yhaa You have done it but next? if YOU Want to your Others Programming Then Follow US HERE and Join Telegram.

Post a Comment