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
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.
To successfully implement a while loop in C, three distinct elements must be explicitly written:
1. Initialization
Must be performed BEFORE entering the loop block to assign an initial value to the loop counter variable.
2. Test Condition
Evaluated at the top before every iteration. If True, the body executes; if False, control exits the loop.
3. Counter Update
Must be written INSIDE the loop body to modify the counter variable, ensuring the condition eventually becomes False.
4. Infinite While Loop
Passing a non-zero constant like 1 keeps the condition permanently True until terminated with a break statement.
Here is a comparative breakdown of how the while loop stands against other C loop structures:
| Loop Type | Control Style | Best Practical Use Case |
|---|---|---|
While Loop | Entry-Controlled (Condition checked before body) | When iteration count is unknown beforehand (e.g., reversing digits, reading files). |
For Loop | Entry-Controlled (Header contains all 3 steps) | When exact iteration count is known in advance (e.g., arrays, fixed counters). |
Do...While Loop | Exit-Controlled (Condition checked after body) | When the loop body must execute at least once (e.g., menu choices, input validation). |
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!
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 1: Evaluate Condition at Top
Execution begins by evaluating the condition inside while(condition).
Step 2: Execute Body Statements
If condition is True (non-zero), control enters the block and executes statements sequentially.
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.
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.
while block (e.g., omitting i++), the condition will remain True permanently, freezing your program in an infinite loop!
- 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.