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

</>
Copy
#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:

  1. We initialize an integer variable i with value 1.
  2. The for loop is written as for (;;), which makes it run indefinitely.
  3. Inside the loop, we print the value of i using printf().
  4. An if statement checks if i equals 5. If true, we use break to exit the loop.
  5. i is 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

</>
Copy
#include <stdio.h>

int main() {
    // Infinite loop
    for (;;) {
        printf("Running... ");
    }

    return 0;
}

Explanation:

  1. The for loop is written as for (;;) without initialization, condition, or increment.
  2. Since no exit condition is specified, the loop runs indefinitely.
  3. The statement inside the loop prints “Running…” continuously.
  4. This loop must be stopped manually (e.g., using Ctrl + C in 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

</>
Copy
#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:

  1. We declare an integer variable num to store user input.
  2. The for (;;) loop runs indefinitely without a condition.
  3. Inside the loop, we prompt the user to enter a number using printf() and read input using scanf().
  4. If the user enters 0, we use break to terminate the loop.
  5. 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:

  1. Example 1: Controlled infinite loop using break to print numbers.
  2. Example 2: Creating an infinite loop that runs indefinitely.
  3. 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.