Omitting Expressions within the for Loop

C allows us to omit some or all of the expressions within the for loop. In this article, we will learn what happens when we omit some or all of these expressions. We will closely observe the behavior of the loop. So, let’s dive in.


Omitting the First Expression

The first expression in the for loop is the initialization expression. Omitting this may cause unexpected behavior. 

For example, in my case when I executed the following program, I didn’t get any output, but this behavior is unpredictable. You may get a weird looking result which you haven’t expected because an uninitialized variable can contain a garbage value or an unpredictable value.

#include<stdio.h>

int main()
{
    int i;
    for(; i < 10; i++) { // variable i is uninitialized
        printf("%d", i);
    }
    return 0;
}

Output:

No Output

Have you noticed the semicolon before the conditional expression? It is mandatory to put the semicolon even if we decide to omit the initialization expression. 

So, if you have decided to omit the initialization expression for some reason (which I strongly recommend you shouldn’t), make sure to initialize the loop variable somewhere before the loop.


Omitting the Second Expression

The second expression in the for loop is the conditional expression. Omitting this expression would cause the loop to run indefinitely (infinite times) because the conditional expression always helps in terminating the for loop.

For example, the following program will continue to run and will never end because the loop condition is missing in the for loop. 

#include<stdio.h>

int main()
{
    int i;
    for(i = 0;; i++) {
        printf("%d", i);
    }
    return 0;
}

Output:

Infinitely many whole numbers

Have you noticed the two semicolons between the two loop expressions? Great 😃. One semicolon marks the end of the initialization expression, while the second semicolon represents the empty loop condition. 

To resolve this problem, you have two choices. Either you add the conditional expression within the parentheses as usual, which is undoubtedly the best way, or you can add an if statement with the condition that would terminate the loop as expected through the break statement. The break statement allows us to terminate the loop whenever we want to based on some condition. 

Example:  Loop without conditional expression

#include<stdio.h>

int main()
{
    int i;
    for(i = 0;; i++) {
        if (i > 10) // if i is greater than 10
            break; // terminate or end the loop
        printf("%d ", i);
    }
    return 0;
}

Output:

0 1 2 3 4 5 6 7 8 9 10

Example:  Loop with conditional expression 

#include<stdio.h>

int main()
{
    int i;
    for(i = 0; i <= 10; i++) {
        printf("%d ", i);
    }
    return 0;
}

Output:

0 1 2 3 4 5 6 7 8 9 10

The output is the same in both the examples, and you may prefer to choose one way over the other based on the problem you are trying to solve.


Omitting the Third Expression 

The third expression of the for loop is also called the increment (or decrement) expression. This expression is responsible for leading us to the termination of the for loop. Without this expression, the loop may run infinite times. The following example program demonstrates the same:

#include<stdio.h>

int main()
{
    int i;
    for(i = 0; i <= 10;) {
        printf("%d ", i);
    }
    return 0;
}

Output:

Infinitely many zeros

Notice that this time, two semicolons are not needed after the conditional expression.

To resolve this problem, you can choose to write increment/decrement expression within the parentheses of the loop (best approach), or within the loop body.

Example program:  increment/decrement expression within parentheses

#include<stdio.h>

int main()
{
    int i;
    for(i = 0; i <= 10; i++) {
        printf("%d ", i);
    }
    return 0;
}

Output:

0 1 2 3 4 5 6 7 8 9 10

Example program: increment/decrement expression within the loop body

#include<stdio.h>

int main()
{
    int i;
    for(i = 0; i <= 10;) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

Output:

0 1 2 3 4 5 6 7 8 9 10

Omitting All Three Expressions

Up to this point, we learned how the loop behaves when we omit some expressions within the parentheses of the loop. But, what happens if we omit all the expressions of the for loop? The loop will run forever

Example Program:

#include<stdio.h>

int main()
{
    for(;;) {
        printf("a");
    }
    return 0;
}

In the above program, the for loop will print a’s infinitely many times because there is nothing to control the execution of the for loop.


Conclusion

The for loop is quite useful when we want to repeat something a certain number of times. Omitting some or all of the expressions within the for loop parentheses may lead to unexpected behavior which can sometimes become difficult to debug. So, make sure to avoid such situations at all costs.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.