In a program to print hello:
for (1; 2; 3)
printf ("Hello");
Why does the output show its an infinite loop? Isn't for ( ; ; ) alone an infinite loop?
In a program to print hello:
for (1; 2; 3)
printf ("Hello");
Why does the output show its an infinite loop? Isn't for ( ; ; ) alone an infinite loop?
Hmm you correct. In C for structure is
for ( initialization; condition; increment/decrement)
So, In your code, for (1; 2; 3) here in condition part (ie. middle) is a non-zero (ie. 2) and it,s always true. That's why your code always get true condition and infinite loop occur.
According to infinite Loop Definition in For Loop:
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for( ; ; ) construct to signify an infinite loop.
For better Understanding, you can follow For Looop
In C, all non-zero integers are considered as true. So here,
for (1; 2; 3)
2 is a non-zero integer and so, it is an infinite loop. And yes,
for ( ; ; )
is also an infinite loop.
for will halt once the second statement evaluates to 0. (An empty statement counts as non-zero in this context).
Yours never does. for(1; 2; 3), for(; 2;), and for(;;) will not halt.