In this article, you will understand how the while loop works, see practical examples with output, and learn when to use it effectively.
Table of Contents
What is a While Loop in C?
A while loop is an entry-controlled loop. This means the condition is checked before executing the loop body. If the condition is true, the loop runs. If it is false, the loop is skipped.Syntax:
Flow of Execution:while (condition) {
// code to be executed
}
- First, the condition is evaluated.
- If it is true, the loop body executes.
- After execution, the condition is checked again.
- This continues until the condition becomes false.
Basic Example of While Loop
Below is the C program to print numbers from 1 to 5:
// C program to print numbers
// from 1 to 5
#include stdio.h
int main()
{
int i = 1;
while (i = 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output:
Explanation:1
2
3
4
5
Here’s the thing: the loop starts with i = 1. The condition i = 5 is checked before every iteration. Inside the loop, the value of i is printed and then incremented. Once i becomes 6, the condition fails and the loop stops.
Example: Sum of First N Numbers
Below is the C program to calculate the sum of the first 5 natural numbers:
// C program to calculate the sum of
// the first 5 natural numbers
#include stdio.h
int main()
{
int i = 1, sum = 0;
while (i = 5)
{
sum = sum + i;
i++;
}
printf("Sum = %d", sum);
return 0;
}
Output:
Explanation:Sum = 15
The loop keeps adding the value of i to sum. After each iteration, i increases. When i exceeds 5, the loop stops, and the final sum is printed.
Example: Infinite While Loop
A while loop can run forever if the condition never becomes false.
// C program to show infinite while loop
#include stdio.h
int main()
{
while (1)
{
printf("This will run forever\n");
}
return 0;
}
Explanation:The condition 1 is always true. So the loop never ends. This type of loop is useful in situations like continuous monitoring systems but must be used carefully.
Example: While Loop with User Input
Let’s take input from the user until they enter 0.
// C program to show while loop
// with user input
#include stdio.h
int main()
{
int num;
printf("Enter numbers (0 to stop):\n");
scanf("%d", &num);
while (num != 0)
{
printf("You entered: %d\n", num);
scanf("%d", &num);
}
return 0;
}
Output:
Explanation:Enter numbers (0 to stop):
2
You entered: 2
0
The loop continues as long as the user does not enter 0. This pattern is very common in real-world programs.
Key Features of While Loop
- Entry-Controlled Loop: The condition is checked before execution, so the loop may not run even once.
- Flexible Condition: You can use any logical or relational expression as the condition.
- Suitable for Unknown Iterations: Use while loops when you do not know how many times the loop should run.
- Simple and Easy to Implement: The syntax of the while loop is straightforward and easy for beginners to understand and use.
- Useful for Condition-Based Execution: The while loop is ideal when execution depends on a condition rather than a fixed number of iterations.
Common Mistakes to Avoid
- Forgetting to Update the Variable: If you do not update the loop variable, the loop may run forever.
- Wrong Condition: A small mistake in the condition can lead to incorrect results or infinite loops.
- Missing Braces: If you write multiple statements without braces, only one statement will be inside the loop.
- Using Assignment Instead of Comparison: Writing = instead of == in the condition can lead to unexpected behavior and logical errors.
- Not Handling Edge Cases: Failing to consider cases like zero, negative values, or invalid input may cause incorrect output or infinite loops.
While Loop vs For Loop
Below is the difference between while loop and for loop:| Feature | While Loop | For Loop |
|---|---|---|
| Condition Check | Before execution | Before execution |
| Use case | Unknown iterations | Known iterations |
| Structure | Simple and flexible | Compact and structured |
| Initialization | Done outside | Done inside |
Conclusion
The while loop in C is one of the most useful tools when you need repetition with flexibility. It works best when the number of iterations is not fixed beforehand. Once you understand how the condition controls execution, writing efficient loops becomes much easier. With practice, you will start recognizing exactly when a while loop is the right choice.Frequently Asked Questions
1. What is a while loop in C?2. When should I use a while loop?A while loop is a control structure that repeatedly executes a block of code as long as a given condition is true.
3. Can a while loop run zero times?You should use a while loop when the number of iterations is not known in advance.
4. What is an infinite loop in C?Yes, if the condition is false initially, the loop body will not execute even once.
5. What is the difference between while and do-while loop?An infinite loop occurs when the condition never becomes false, causing the loop to run forever.
A while loop checks the condition before execution, whereas a do-while loop executes at least once before checking the condition.
0 Comments