While Loop in C Language: Syntax, Programs & Rules (Unit 2)



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

While Loop in C Language — BKNMU Junagadh

Syntax, Entry-Controlled Execution, Practical Digit Manipulation Programs, and Common Pitfalls

BKNMU Junagadh NEP 2020 Unit 2 While Loop Entry-Controlled Indefinite Iteration
In simple words: Welcome to Unit 2! The while loop is an entry-controlled iteration statement in C programming. It repeatedly executes a block of target statements as long as a given boolean condition evaluates to True (non-zero). It is ideal when the exact number of iterations is not known beforehand.
📖 Core Components of a While Loop

To successfully implement a while loop in C, three distinct elements must be explicitly written:

i = 1;

1. Initialization

Must be performed BEFORE entering the loop block to assign an initial value to the loop counter variable.

while (condition)

2. Test Condition

Evaluated at the top before every iteration. If True, the body executes; if False, control exits the loop.

i++;

3. Counter Update

Must be written INSIDE the loop body to modify the counter variable, ensuring the condition eventually becomes False.

while (1)

4. Infinite While Loop

Passing a non-zero constant like 1 keeps the condition permanently True until terminated with a break statement.

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

Here is a comparative breakdown of how the while loop stands against other C loop structures:

Loop TypeControl StyleBest Practical Use Case
While LoopEntry-Controlled (Condition checked before body)When iteration count is unknown beforehand (e.g., reversing digits, reading files).
For LoopEntry-Controlled (Header contains all 3 steps)When exact iteration count is known in advance (e.g., arrays, fixed counters).
Do...While LoopExit-Controlled (Condition checked after body)When the loop body must execute at least once (e.g., menu choices, input validation).
ℹ️ Minimum Execution Count: Because a while loop checks its test condition at the very beginning (entry point), it has a minimum execution count of 0. If the condition starts off False, the loop body is skipped entirely!
💻 Code Examples & Practical Applications

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

#include <stdio.h>

int main() {
    int i = 1; // 1. Initialization

    // 2. Test Condition at entry
    while (i <= 5) {
        printf("Number: %d\n", i);
        i++; // 3. Counter Update
    }

    return 0;
}

2. Classic Exam Program: Reverse a Number:

#include <stdio.h>

int main() {
    int num = 1234, rev = 0, rem;

    // Runs until all digits are processed
    while (num != 0) {
        rem = num % 10;
        rev = (rev * 10) + rem;
        num = num / 10;
    }

    printf("Reversed Number: %d\n", rev);

    return 0;
}

3. Sum of Digits of a Number:

#include <stdio.h>

int main() {
    int num = 582, sum = 0;

    while (num > 0) {
        sum += num % 10;
        num /= 10;
    }

    printf("Sum of digits: %d\n", sum);

    return 0;
}
🔄 Step-by-Step Execution Flow
1

Step 1: Evaluate Condition at Top

Execution begins by evaluating the condition inside while(condition).

2

Step 2: Execute Body Statements

If condition is True (non-zero), control enters the block and executes statements sequentially.

3

Step 3: Update Variable & Loop Back

After reaching the end of the body, control automatically loops back to Step 1 to re-check the condition.

💡 Exam Tip on No-Semicolon Rule: Never place a semicolon directly after the while condition line like while(i <= 10); unless you intentionally want an empty loop! A trailing semicolon terminates the loop statement right there and causes an infinite loop.
⚠️ Forgetting Increment/Decrement: If you forget to update your counter variable inside the while block (e.g., omitting i++), the condition will remain True permanently, freezing your program in an infinite loop!
📝 Quick Revision — Key Exam Points
  • Loop Category: Entry-Controlled Iteration Statement.
  • Minimum Executions: 0 times (if initial condition is False).
  • Header Syntax: while (condition) { /* body */ }
  • Common Exam Programs: Reversing numbers, Armstrong numbers, Sum of digits, Palindrome check.
  • Semicolon Rule: Do NOT put a semicolon after while(condition) header.

Post a Comment

Thanks for comment.

Previous Post Next Post