C Notes
Master the while loop in C with syntax, flowchart, counter-controlled loops, sentinel-controlled loops, infinite loops, and practical examples. Includes interview questions.
The while loop is one of the three loop structures in C that allows you to repeat a block of code as long as a condition remains true. It's called an entry-controlled loop because the condition is checked before the loop body executes. If the condition is false from the very beginning, the loop body never runs — not even once.
Loops are essential for tasks that involve repetition: printing patterns, reading input until a sentinel value, processing arrays, counting, summing — the list goes on. The while loop is particularly useful when you don't know in advance how many times you need to iterate.
Syntax of While Loop
while (condition) {
// loop body - executes repeatedly
// must include something that eventually makes condition false
}| Component | Description |
|---|---|
condition | Expression checked before each iteration |
| Loop body | Statements executed on each iteration |
| Update | Something inside that changes the condition (prevents infinite loop) |
Flowchart of While Loop
Wait — let me redraw that more clearly:
Basic Example: Counting
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Loop ended. count = 6
Types of While Loops
1. Counter-Controlled Loop
The number of iterations is known or determined by a counter variable.
Sum of 1 to 10 = 55
2. Sentinel-Controlled Loop
The loop continues until a special value (sentinel) is encountered. You don't know how many iterations in advance.
Enter numbers (enter -1 to stop): 10 20 30 40 -1 Total numbers entered: 4 Sum: 100 Average: 25.00
3. Flag-Controlled Loop
A boolean flag variable controls when the loop ends.
#include <stdio.h>
int main() {
int num, found = 0;
int target = 7;
int guess;
printf("Guess the number (1-10): ");
while (!found) {
scanf("%d", &guess);
if (guess == target) {
found = 1;
printf("Correct! You guessed it!\n");
} else if (guess < target) {
printf("Too low! Try again: ");
} else {
printf("Too high! Try again: ");
}
}
return 0;
}Guess the number (1-10): 3 Too low! Try again: 9 Too high! Try again: 7 Correct! You guessed it!
Practical Examples
Example 1: Reverse a Number
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
int original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Original: %d\n", original);
printf("Reversed: %d\n", reversed);
return 0;
}Enter a number: 12345 Original: 12345 Reversed: 54321
Example 2: Count Digits in a Number
Enter a number: 98765 Number of digits in 98765: 5
Example 3: Fibonacci Series
How many Fibonacci terms? 8 Fibonacci Series: 0 1 1 2 3 5 8 13
Infinite While Loop
An infinite loop runs forever (until externally stopped or a break statement is hit). It's created by using a condition that's always true:
#include <stdio.h>
int main() {
// Infinite loop - use Ctrl+C to stop
while (1) {
printf("This runs forever!\n");
}
return 0; // Never reached
}Practical use of infinite loops:
#include <stdio.h>
int main() {
int choice;
while (1) { // Infinite loop with break to exit
printf("\n1. Say Hello\n");
printf("2. Say Goodbye\n");
printf("3. Exit\n");
printf("Choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Hello, World!\n");
} else if (choice == 2) {
printf("Goodbye, World!\n");
} else if (choice == 3) {
printf("Exiting program...\n");
break; // Exit the infinite loop
} else {
printf("Invalid choice!\n");
}
}
return 0;
}Common Mistakes
Mistake 1: Forgetting to Update the Loop Variable
Mistake 2: Off-by-One Errors
Mistake 3: Semicolon After while
While Loop vs For Loop
| Aspect | While Loop | For Loop |
|---|---|---|
| Best for | Unknown iterations | Known iterations |
| Initialization | Before the loop | Inside loop syntax |
| Update | Inside loop body | Inside loop syntax |
| Readability | Better for sentinel/flag loops | Better for counting |
| Flexibility | More flexible structure | More compact syntax |
Interview Questions
Q1: What is an entry-controlled loop?
An entry-controlled loop checks the condition before executing the loop body. The while loop and for loop in C are entry-controlled — if the condition is false initially, the body never executes.
Q2: How do you create an infinite loop using while? When is it useful?
Use while(1) or while(true). Infinite loops are useful for event-driven programs, servers that need to run continuously, menu-driven programs with explicit exit conditions, and embedded systems that run forever.
Q3: What is the difference between while(1) and while(0)?
while(1) creates an infinite loop that runs forever because 1 is always true. while(0) creates a loop that never executes because 0 is always false — the body is completely skipped.
Q4: Can a while loop have an empty body?
Yes. while(condition); is valid (note the semicolon). This is sometimes used to wait for a condition to change (busy waiting) or to consume input, though it wastes CPU cycles.
Q5: What happens if the condition uses a variable that's never modified inside the loop?
If the condition variable doesn't change and the condition starts as true, you get an infinite loop. If it starts as false, the loop never executes. Either way, it's usually a bug.
Summary
The while loop is a versatile, entry-controlled loop that repeats code as long as its condition is true. It's ideal for situations where the number of iterations isn't known in advance — sentinel-controlled input, flag-based loops, and event-driven repetition. Always ensure your loop has a way to terminate (updating the condition variable), watch out for off-by-one errors, and never put a semicolon after the while condition. The while loop, combined with for and do-while, gives you complete control over repetitive execution in C.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for While Loop in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, while, loop, while loop in c programming
Related C Programming Topics