Sushmitha
Sushmitha

Reputation: 13

Is it possible to use for loop with misleading syntax?

Syntax of for loop:

for(initialization;condition;increment/decrement){
statements;
}

Why is the below code running infinite times and then why there is no syntax error?

int x=10;
for(x++;x++;x++){
printf("\n %d",x);
}

Upvotes: 1

Views: 91

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

The for loop is defined in the C Standard in two ways

for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

So this for loop

for ( x++; x++; x++ ){

has a correct form that corresponds to the first definition of the for loop.

The loop will be executed until the value x after the third expression x++ will be equal to 0.

If instead of the type int you will use the type unsigned int you will get a correct finite loop because there will not be an overflow that results in undefined behavior for signed int types.

unsigned int x=10;
for(x++;x++;x++){
printf("\n %d",x);
}

To make the loop more clear let's use the initial value of the variable x in your example will be equal to -3.

int x = -3;
for ( x++; x++; x++ ){
    printf("\n %d",x);
}

So after the first expression x++ x will be equal to -2, SO as -2 is not equal to 0 the body of the loop will get the control and the printf statement will output -1 because x was incremented also in the condition.

Then the third expression x++ will evaluated. And x will be equal to 0.

So now the condition x++ will be evaluated to logical false because it is the value of x before increment.

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

Upvotes: 1

Why is the below code running infinite times...?

Because the used condition doesn't stop it from executing since it usually never will evaluate to 0.

x is incremented twice at each iteration until it reaches the value defined in the macro INT_MAX (limits.h). After that point, incrementing x invokes undefined behavior.

...and then why there is no syntax error?

x++ is a valid condition and the compiler doesn't check whether the expression you set as condition makes sense or not. It only checks for syntax errors. So the program can be compiled and when running, the loop is infinitely.

So to answer:

Is it possible to use for loop with misleading syntax?

The answer is: Yes. But in this case it will invoked undefined behavior as said above at some point.

Upvotes: 3

Lundin
Lundin

Reputation: 213842

The 3 clauses of the for loop are optional and the only requirement is that they should be valid expressions - the first one could optionally contain a variable declaration. The syntax is valid.

In the 2nd clause the result of the expression is used as a check against zero. You start at 10, so the first check reads the value 10, and so on. It is never zero so the loop won't terminate.

Other than that, there is a sequence point after each for clause, so the code is also valid as far as sequencing goes. This code will of keep counting up until it hits integer overflow, which is undefined behavior.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409176

The "misleading syntax" you use might be weird and bad, but it's still valid. That's why you don't get an error.

The three clauses in the loop are general expressions, with an exception for the first (the "initialization" expression) which is allowed to be a variable definition.

The reason you get an "infinite" loop is because x++ is never zero (which is the value representing "false"). It will however lead to undefined behavior as it will lead to an arithmetic overflow when you increment beyond the limit of int.


It might be easier to understand what's going on if you translate the for loop into the corresponding while loop (all for loops can be translated as while loops).

The for loop

for (initialization; condition; increment/decrement)
{
    statements;
}

can be translated to the while loop

initialization;
while (condition)
{
    statements;
    increment/decrement;
}

If we now do the same translation for the problematic code:

int x = 10;
for (x++; x++; x++)
{
    printf("\n %d",x);
}

it becomes

int x = 10;
x++;  // initialization
while (x++)  // condition
{
    printf("\n %d",x);  // statements
    x++;  // increment/decrement
}

This might make it clearer what's going on and why you get an "infinite" loop.

Upvotes: 2

Related Questions