For Loop Without Condition in C
In C, a for loop can be written without a condition by leaving the condition field empty. This creates an infinite loop that continues execution unless explicitly stopped using a break statement or an external condition inside the loop body. Such loops are useful in scenarios where the exit condition is determined dynamically within the loop.
Examples of For Loop Without Condition
1. Printing Numbers Using a For Loop Without Condition
In this example, we use a for loop without a condition to print numbers from 1 to 5. The loop will run indefinitely, but we will use an if statement and break to stop execution when a condition is met.
main.c
#include <stdio.h>
int main() {
// Initializing variable
int i = 1;
// For loop without condition
for (;;) {
printf("%d ", i);
// Break the loop when i reaches 5
if (i == 5) {
break;
}
i++; // Increment i
}
return 0;
}
Explanation:
- We initialize an integer variable
iwith value1. - The
forloop is written asfor (;;), which makes it run indefinitely. - Inside the loop, we print the value of
iusingprintf(). - An
ifstatement checks ifiequals 5. If true, we usebreakto exit the loop. iis incremented in each iteration to ensure the loop terminates after 5 iterations.
Output:
1 2 3 4 5
2. Infinite Loop Using a For Loop Without Condition
In this example, we demonstrate an infinite loop using a for loop without a condition. The loop will continuously print “Running…” unless manually stopped.
main.c
#include <stdio.h>
int main() {
// Infinite loop
for (;;) {
printf("Running... ");
}
return 0;
}
Explanation:
- The
forloop is written asfor (;;)without initialization, condition, or increment. - Since no exit condition is specified, the loop runs indefinitely.
- The statement inside the loop prints “Running…” continuously.
- This loop must be stopped manually (e.g., using
Ctrl + Cin the terminal).
Output:
Running... Running... Running... (infinite loop)
3. Using a For Loop Without Condition to Iterate Until User Input
In this example, we keep prompting the user for input until they enter the number 0. The loop will run indefinitely until the user provides a stopping input.
main.c
#include <stdio.h>
int main() {
int num;
// Infinite loop until user enters 0
for (;;) {
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
if (num == 0) {
break;
}
printf("You entered: %d\n", num);
}
return 0;
}
Explanation:
- We declare an integer variable
numto store user input. - The
for (;;)loop runs indefinitely without a condition. - Inside the loop, we prompt the user to enter a number using
printf()and read input usingscanf(). - If the user enters
0, we usebreakto terminate the loop. - Otherwise, the entered number is displayed, and the loop repeats.
Output:
Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 10
You entered: 10
Enter a number (0 to stop): 0
Conclusion
In this tutorial, we explored different ways to use a for loop without a condition in C:
- Example 1: Controlled infinite loop using
breakto print numbers. - Example 2: Creating an infinite loop that runs indefinitely.
- Example 3: Using an infinite loop to prompt user input until they enter
0.
By omitting the condition in a for loop, we can create flexible loops where the stopping condition is defined inside the loop body.
