The while
loop in C is a control flow statement that repeatedly executes a block of code as long as a specified condition is true. It is one of the most commonly used loops in C programming for scenarios where the number of iterations is not known beforehand.
Syntax
while (condition) { // Code to be executed repeatedly }
condition
: An expression that is evaluated before each iteration. If it evaluates to true (non-zero), the loop continues. If it evaluates to false (zero), the loop terminates.- The code inside the loop will execute as long as the condition is true.
Example 1: Basic while
Loop
#include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("Iteration %d\n", i); i++; // Increment i to avoid an infinite loop } return 0; }
Output:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5
In this example, the loop starts with i = 1
. As long as i
is less than or equal to 5, the loop prints the value of i
and then increments i
by 1 after each iteration.
Example 2: Infinite while
Loop (Avoiding Infinite Loops)
A while
loop can potentially run infinitely if the condition is always true. Here’s an example of an infinite loop:
#include <stdio.h> int main() { while (1) { // Infinite loop (1 is always true) printf("This will run forever unless interrupted.\n"); } return 0; }
To stop an infinite loop, you would need to manually break it (e.g., using Ctrl+C
in the terminal).
Example 3: Using break
to Exit a while
Loop
You can use the break
statement to exit a while
loop prematurely based on a condition.
#include <stdio.h> int main() { int i = 1; while (i <= 10) { if (i == 5) { break; // Exit the loop when i equals 5 } printf("Iteration %d\n", i); i++; } return 0; }
Output:
Iteration 1 Iteration 2 Iteration 3 Iteration 4
In this example, the loop will terminate when i
becomes 5, thanks to the break
statement.
Example 4: Using continue
in a while
Loop
The continue
statement skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
#include <stdio.h> int main() { int i = 1; while (i <= 5) { if (i == 3) { i++; // Increment i to avoid infinite loop continue; // Skip printing when i equals 3 } printf("Iteration %d\n", i); i++; } return 0; }
Output:
Iteration 1 Iteration 2 Iteration 4 Iteration 5
Here, when i
equals 3, the continue
statement is executed, which skips the printf
statement for that iteration.
Example 5: Using while
with User Input
A while
loop can be useful when you want to repeat an action based on user input.
#include <stdio.h> int main() { int number; printf("Enter a positive number: "); scanf("%d", &number); while (number > 0) { printf("You entered: %d\n", number); printf("Enter another positive number (or a non-positive number to quit): "); scanf("%d", &number); } printf("You entered a non-positive number, exiting the loop.\n"); return 0; }
Output:
Enter a positive number: 5 You entered: 5 Enter another positive number (or a non-positive number to quit): 3 You entered: 3 Enter another positive number (or a non-positive number to quit): -1 You entered a non-positive number, exiting the loop.
In this example, the loop continues to prompt the user to enter positive numbers and prints them until a non-positive number is entered, at which point the loop terminates.
Summary
- The
while
loop repeats a block of code as long as a specified condition is true. - The condition is evaluated before each iteration, meaning the loop may not execute at all if the condition is initially false.
- It is essential to modify the condition inside the loop (e.g., by updating variables) to avoid infinite loops.
- The
break
statement can be used to exit the loop prematurely, andcontinue
skips the current iteration and moves to the next.