Welcome to The Coding College! In this tutorial, we’ll dive into the while
loop in C programming, a fundamental construct for executing repetitive tasks efficiently.
What is a while
Loop?
A while
loop is a control flow statement that allows a block of code to be executed repeatedly, as long as a specified condition is true. It’s ideal when the number of iterations is not known beforehand.
Syntax
while (condition) {
// Code to execute as long as the condition is true
}
Key Points:
- Condition: The loop evaluates the condition before executing the code block.
- Execution: If the condition is true, the code block executes. If false, the loop terminates.
- Infinite Loop: Ensure the condition eventually becomes false to avoid infinite loops.
Example 1: 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:
1
2
3
4
5
Example 2: Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
i++;
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Output:
Enter a positive integer: 5
Sum of first 5 natural numbers is: 15
Example 3: Check Even or Odd Numbers in a Range
#include <stdio.h>
int main() {
int start, end;
printf("Enter start and end of range: ");
scanf("%d %d", &start, &end);
printf("Even numbers in the range:\n");
while (start <= end) {
if (start % 2 == 0) {
printf("%d\n", start);
}
start++;
}
return 0;
}
Output:
Enter start and end of range: 2 10
Even numbers in the range:
2
4
6
8
10
Handling Infinite Loops
An infinite loop occurs when the condition in a while
statement never becomes false. Here’s an example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
// Uncomment the next line to avoid an infinite loop
// i++;
}
return 0;
}
Solution: Always update variables involved in the condition to prevent infinite loops.
Use Cases of while
Loops
- Reading User Input: Keep asking for input until it is valid.
- Processing Data: Work through a dataset or file until the end.
- Real-Time Monitoring: Continuously check conditions in embedded systems or games.
Best Practices
- Initialize Variables Properly: Ensure variables in the condition are initialized to avoid undefined behavior.
- Prevent Infinite Loops: Update the condition variable or include a termination condition within the loop.
- Keep the Body Simple: Avoid overly complex logic in the loop body for better readability.
Advanced Example: Factorial Calculation
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
int i = 1;
while (i <= num) {
factorial *= i;
i++;
}
printf("Factorial of %d is: %d\n", num, factorial);
return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 is: 120
Difference Between while
and do...while
Aspect | while Loop | do...while Loop |
---|---|---|
Condition Check | Condition is checked before execution. | Condition is checked after execution. |
Minimum Execution | Executes 0 or more times. | Executes at least once. |
Conclusion
The while
loop is an essential construct in C programming for handling repetitive tasks where the number of iterations is not predefined. By mastering its usage, you can write efficient and readable code.