-
Hajipur, Bihar, 844101
In C programming, loops are essential for repeating a block of code multiple times. The while loop is one of the simplest and most widely used loops. It allows a program to execute a block of statements repeatedly as long as a specified condition remains true.
The while loop is particularly useful when the number of iterations is not known beforehand. This makes it different from for loops, which are generally used when the number of repetitions is fixed.
A while loop checks a condition before executing the block of code. If the condition is true, the code inside the loop runs. After each iteration, the condition is checked again. This process continues until the condition becomes false.
Syntax:
while(condition) {
// code to execute repeatedly
}
condition: A Boolean expression that controls the loop.
The loop executes repeatedly as long as the condition is true.
If the condition is false initially, the loop may not execute at all.
#include <stdio.h>
int main() {
int i = 1;
while(i <= 5) {
printf("Iteration %d\n", i);
i++; // increment to avoid infinite loop
}
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Here, the loop starts with i = 1. The condition i <= 5 is true initially, so the loop executes. After each iteration, i is incremented by 1. Once i becomes 6, the condition becomes false, and the loop stops.
Pre-test loop: The condition is checked before executing the block of code.
Infinite loop risk: Forgetting to update variables inside the loop can lead to infinite loops.
Dynamic repetition: Useful when the number of iterations depends on user input or calculations.
Control with variables: A loop variable or condition controls the number of repetitions.
The while loop can be used to repeatedly prompt a user until a valid input is provided:
#include <stdio.h>
int main() {
int number;
printf("Enter a positive number: ");
scanf("%d", &number);
while(number <= 0) {
printf("Invalid input. Enter a positive number: ");
scanf("%d", &number);
}
printf("You entered %d\n", number);
return 0;
}
The program keeps asking for a positive number until the user enters one.
The loop exits only when the condition becomes false (number > 0).
Sometimes, infinite loops are intentional, for example, in programs that run continuously until the user stops them:
#include <stdio.h>
int main() {
while(1) {
printf("This will run forever.\n");
}
return 0;
}
Here, 1 always evaluates to true, so the loop never ends.
Use break inside infinite loops to exit under certain conditions.
Break: Exits the loop immediately.
Continue: Skips the rest of the current iteration and checks the condition again.
Example:
#include <stdio.h>
int main() {
int i = 1;
while(i <= 10) {
if(i == 5) {
i++;
continue; // skip printing 5
}
if(i == 8) {
break; // stop loop at 8
}
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
6
7
continue skips iteration when i == 5.
break stops the loop when i == 8.
You can use a while loop inside another while loop to handle more complex tasks, such as printing patterns:
#include <stdio.h>
int main() {
int i = 1;
while(i <= 3) {
int j = 1;
while(j <= i) {
printf("* ");
j++;
}
printf("\n");
i++;
}
return 0;
}
Output:
*
* *
* * *
Nested loops are useful for tables, patterns, and multi-dimensional problems.
Always update loop variables: Prevent infinite loops.
Use clear conditions: Avoid overly complex Boolean expressions.
Break long loops into smaller functions: Improves readability.
Use break and continue sparingly: Avoid confusing the flow of logic.
Test edge cases: Ensure the loop works correctly when the condition is false initially.
#include <stdio.h>
int main() {
int number, sum = 0;
printf("Enter numbers (0 to stop): \n");
scanf("%d", &number);
while(number != 0) {
sum += number;
scanf("%d", &number);
}
printf("Total sum: %d\n", sum);
return 0;
}
The loop continues to read numbers until the user enters 0.
All positive numbers are added to the sum variable.
The while loop in C is a pre-test loop that repeatedly executes a block of code as long as a condition is true. It is ideal when the number of iterations is unknown or dynamic. Understanding how to use break, continue, and nested loops makes your programs more flexible and powerful. Proper management of the loop condition and variables ensures efficient and error-free execution.
Write a program to print numbers from 1 to 10 using a while loop.
Create a program to calculate the sum of the first N natural numbers, where N is entered by the user.
Write a program to print all even numbers between 1 and 50 using a while loop.
Develop a program to repeatedly ask the user for a positive number until they enter a valid one.
Write a program to calculate the factorial of a number entered by the user using a while loop.
Create a program to print a multiplication table of a number using a while loop.
Write a program to reverse a number entered by the user using a while loop.
Develop a program to find the sum of digits of a number using a while loop.
Write a program to continuously take input numbers from the user and stop when 0 is entered, then print the total sum.
Create a program to print a right-angled triangle pattern of stars using nested while loops.