Do...While Loop in C Language: Syntax, Examples & Rules (Unit 2)



BCA Sem 1  |  Unit 2  |  Computer Concepts & C Programming

Do...While Loop in C Language — BKNMU Junagadh

Syntax, Exit-Controlled Looping Mechanism, Menu-Driven Applications, and Comparison with While Loop

BKNMU Junagadh NEP 2020 Unit 2 Do-While Loop Exit-Controlled Menu Driven
In simple words: Welcome to Unit 2! The do...while loop is an exit-controlled iteration statement in C programming. Unlike for and while loops, it evaluates its test condition at the end of the loop body, guaranteeing that the loop statements execute at least once regardless of whether the initial condition is True or False.
📖 Core Features of Do...While Loop

The do...while loop is built around four fundamental concepts that distinguish it from other C loops:

do { ... }

1. Body First Execution

The loop body executes immediately upon entry before checking any condition, ensuring a minimum of one execution cycle.

... while(cond);

2. Bottom Condition Check

The condition is tested at the end of every cycle. If True (non-zero), execution loops back to the top of the do block.

while(cond);

3. Mandatory Semicolon

Unlike other control structures, a semicolon (;) is strictly required right after the closing condition parenthesis.

Menu Systems

4. Interactive Applications

Ideal for menu-driven programs where options must be shown to the user at least once before asking to exit or continue.

1️⃣ Master Comparison Table: While vs. Do...While

Here is a detailed comparative summary between entry-controlled while loops and exit-controlled do...while loops in C:

Comparison AspectWhile Loop (while)Do...While Loop (do...while)
Control TypeEntry-Controlled: Evaluates condition before executing loop body.Exit-Controlled: Evaluates condition after executing loop body.
Minimum Executions0 times (if the initial condition is False, body is skipped entirely).1 time (guaranteed to execute at least once even if initial condition is False).
Semicolon UsageNo semicolon after loop header (e.g., while(cond)).Requires a semicolon at the end (e.g., while(cond);).
Primary Use CaseWhen iteration count depends entirely on a pre-checked condition.Interactive user menus, input validation, and user prompt prompts.
ℹ️ Why Use Do...While? Use a do...while loop whenever you need to process input or display options before checking if the action should be repeated—such as repeatedly asking a user for password entry until it matches!
💻 Code Examples & Practical Applications

1. Basic Do...While Loop (Printing Numbers 1 to 5):

#include <stdio.h>

int main() {
    int i = 1;

    // Guaranteed first run before condition test
    do {
        printf("Value of i: %d\n", i);
        i++;
    } while (i <= 5);

    return 0;
}

2. Execution Proof (Condition Starts False):

#include <stdio.h>

int main() {
    int num = 100;

    // Executes once even though num < 10 is False!
    do {
        printf("This prints at least ONCE! num = %d\n", num);
    } while (num < 10);

    return 0;
}

3. Practical Menu-Driven Calculator Example:

#include <stdio.h>

int main() {
    int choice;

    do {
        printf("\n--- BCA SCHOOL MENU ---\n");
        printf("1. Display Hello\n");
        printf("2. Display Unit 2 Notes\n");
        printf("3. Exit Program\n");
        printf("Enter your choice (1-3): ");
        scanf("%d", &choice);

        if (choice == 1) {
            printf("Hello, Student!\n");
        } else if (choice == 2) {
            printf("Unit 2 covers Decision Control & Loops.\n");
        }
    } while (choice != 3);

    printf("Exited successfully.\n");
    return 0;
}
🔄 Step-by-Step Execution Sequence
1

Step 1: Execute Loop Body

Control flows directly into the do block and executes all internal statements sequentially.

2

Step 2: Update Counter / Process Inputs

Variables inside the block are updated or user input is accepted.

3

Step 3: Evaluate Bottom Condition

The expression in while(condition); is tested. If True (non-zero), execution jumps back to Step 1. If False (zero), the loop terminates.

💡 Exam Tip on Syntax: GTU and BKNMU paper evaluators frequently dock marks for missing the trailing semicolon! Always write: do { ... } while (condition); — note the semicolon after the parenthesis.
⚠️ Infinite Loop Warning: If you forget to update the loop counter variable or condition status inside the do body, the loop condition will stay True permanently, leading to an infinite execution loop!
📝 Quick Revision — Key Exam Points
  • Loop Type: Exit-Controlled Iteration Statement.
  • Minimum Executions: Exactly 1 execution cycle guaranteed.
  • Key Syntax Rule: Ends with a mandatory trailing semicolon (while(condition);).
  • Condition Position: Bottom of the loop structure.
  • Best Practical Use: Interactive menu systems and user input validation loops.

Post a Comment

Thanks for comment.

Previous Post Next Post