// example
#include<stdio.h>
int main(){
int i,j;
for(i<4; j=3; j=0){
printf("%d",i);
}
}
// why the output is an infinite for loop with i=1
// example
#include<stdio.h>
int main(){
int i,j;
for(i<4; j=3; j=0){
printf("%d",i);
}
}
// why the output is an infinite for loop with i=1
Since for
loop of form
for (initialization; condition; increment) {/*body*/}
Can be transformed to the while
loop, like so:
{
initialization;
while (condition)
{
/*body*/
increment;
}
}
Your program, can be, effectively, transformed to:
int i,j;
{
i<4;
while (j=3)
{
printf("%d",i);
j=0;
}
}
Since assignment operator returns the value, that is assigned (in this case: 3
), and any non-zero integer value is evaluated to true
, you get an infinite loop.
As to what output you get.. It is undefined behavior, due to you using uninitialized variable i
.
in for
the loop condition part, j=3
become always true. that is why you got an infinite result.
As only the middle condition will be compared and there will always be something not 0 it will continue on and on!