Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've already spent some time with while loops, and they're great for most things. But every once in a while, you'll run into a scenario where the logic feels... clunky. Specifically, when you need to execute a block of code at least once before you even know if you need to loop at all. The classic example is user input validation.

The Maintenance Headache of Duplicated Logic

Imagine you're building a simple menu for a tool where the user has to pick an option between 1 and 3. If you use a standard while loop, you have a problem: the loop condition depends on a variable that hasn't been initialized with user input yet. The naive way to solve this is to "prime" the loop by writing the input logic twice.

int choice;
printf("Please enter a choice (1-3): ");
scanf("%d", &choice);

while (choice < 1 || choice > 3) {
    printf("Invalid input. Please enter a choice (1-3): ");
    scanf("%d", &choice);
}

Now, this works perfectly fine in a small script. But as a professional, this should make you twitch. Why? Because you've duplicated your printf and scanf calls. If your boss tells you to change the prompt to "Select an option (1-3) to continue," you have to remember to change it in two different places. In a larger project, this kind of repetition is where bugs hide—you update the logic in the loop but forget to update the "priming" code above it, and suddenly your program behaves inconsistently.

Guaranteed Execution with the Post-Condition Check

This is exactly where the do-while loop comes in. It flips the script: instead of checking the condition at the top (the pre-condition), it checks it at the bottom (the post-condition). I like to think of it as "do this first, then ask if we should do it again."

int choice;
do {
    printf("Please enter a choice (1-3): ");
    scanf("%d", &choice);
} while (choice < 1 || choice > 3);

Notice how much cleaner that is. We've eliminated the redundancy entirely. The code inside the do block is guaranteed to run at least once, which is exactly what we want because we can't possibly validate a choice that the user hasn't typed yet. It keeps the logic centralized; if you need to change the input method or the prompt text, you do it in one place.

When to Stick With a Standard While Loop

I don't want you to start using do-while for everything. The trade-off is that you lose the ability to skip the loop entirely. If there is any scenario where the loop body should never run based on the initial state of the program, a do-while is the wrong tool. For instance, if you're reading lines from a file that might be empty, a do-while would attempt to process the first line before realizing the file is empty, likely leading to a crash or a garbage value. In those cases, the pre-check of a standard while loop is your safety net.




📋 Practical Task

Build a Secure PIN Retry System

Write a program that simulates a secure ATM PIN entry. The program should have a predefined correct PIN (e.g., 1234). Using a do-while loop, prompt the user to enter their PIN. If the PIN is incorrect, tell them it was wrong and let them try again. The loop should terminate when the user enters the correct PIN. Once the loop exits, print a success message saying "Access Granted."

Rating
0 0

There are no comments for now.

to be the first to leave a comment.