while Loop in C
while Loop in C
Introduction
while Loop in C is a looping statement used to execute a block of code repeatedly as long as a condition remains true. Loops help programmers automate repetitive tasks and reduce unnecessary code repetition.
In this C Programming Course in Jaipur, students learn how the while loop works, its syntax, flow of execution, and real-world applications in C programming.
What is while Loop in C?
The while loop repeatedly executes a block of code while a given condition is true.
Syntax:
while(condition) {
// code
}
The condition is checked before every iteration.
If the condition is true:
- The loop executes
If the condition is false:
- The loop stops
Flow of while Loop
Condition Checked → True → Execute Loop Body → Repeat
Condition Checked → False → Exit Loop
The while loop is also called:
Entry Controlled Loop
because the condition is checked before execution.
Example of while Loop
#include<stdio.h>
int main() {
int i = 1;
while(i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
Understanding the Example
Variable Initialization
int i = 1;
The loop counter variable starts from 1.
Condition
i <= 5
The loop continues until the condition becomes false.
Increment
i++;
The counter increases after every iteration.
Infinite while Loop
If the condition never becomes false, the loop runs forever.
Example:
while(1) {
printf("Infinite Loop");
}
This creates an infinite loop.
Example Using User Input
#include<stdio.h>
int main() {
int number, i = 1;
printf("Enter a number: ");
scanf("%d", &number);
while(i <= 10) {
printf("%d\n", number * i);
i++;
}
return 0;
}
Output for input 2:
2
4
6
8
10
12
14
16
18
20
Applications of while Loop
The while loop is used in:
- Menu-driven applications
- ATM systems
- Login systems
- Data processing
- Game development
- Repeated calculations
Students learning C Programming Course in Jaipur use loops extensively in practical projects.
Difference Between while Loop and if Statement
| while Loop | if Statement |
|---|---|
| Repeats execution | Executes once |
| Used for repetition | Used for decision making |
| Condition checked repeatedly | Condition checked once |
Nested while Loop
A while loop inside another while loop is called nested while loop.
Example:
#include<stdio.h>
int main() {
int i = 1;
while(i <= 3) {
int j = 1;
while(j <= 2) {
printf("%d %d\n", i, j);
j++;
}
i++;
}
return 0;
}
Output:
1 1
1 2
2 1
2 2
3 1
3 2
Common Errors in while Loop
Missing Increment
Incorrect:
while(i <= 5) {
printf("%d", i);
}
This creates an infinite loop.
Correct:
i++;
Incorrect Condition
Incorrect:
while(i >= 5)
when i starts from 1.
The loop never executes.
Best Practices
- Initialize variables properly
- Update loop counter correctly
- Avoid infinite loops
- Write meaningful conditions
- Keep loops simple and readable
Good loop design improves program efficiency.
Importance of while Loop in C
The while loop helps:
- Automate repetitive tasks
- Reduce code duplication
- Improve program efficiency
- Handle repeated operations dynamically
Loops are essential in programming and software development.
Real-World Example
ATM PIN validation:
while(pin != correctPin) {
printf("Enter PIN Again");
}
The loop continues until the correct PIN is entered.
Summary
while Loop in C is an entry-controlled loop used to execute code repeatedly while a condition remains true. It is useful for repetitive tasks and dynamic programming operations.
This lesson explained syntax, flow of execution, examples, nested loops, applications, common errors, and best practices for while loops in C programming.
FAQs
What is while Loop in C?
The while loop repeatedly executes code while a condition is true.
Why is while loop called entry controlled loop?
Because the condition is checked before loop execution.
What is an infinite loop?
An infinite loop runs continuously without stopping.
What is nested while loop?
A while loop inside another while loop is called nested while loop.
Why are loops important in programming?
Loops automate repetitive tasks and improve coding efficiency.
