C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
24: Do-While Loops
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."
There are no comments for now.