I tried out x+=1
, x=x+1
inside the while loop's condition.
x = 0 // Initial value of x
1st case
while(x+=1 && x < 5){
cout << x << endl;
}
2nd case
while(x=x+1 && x < 5){
cout << x << endl;
}
The first case and second case are puzzling. What is the difference in the behavior of x+=1
and x=x+1
that both go into infinite loop (Because of short circuiting). But the value of x in the 1st case is stuck at 1.
Any thoughts ?
According to Is x += 1 more efficient than x = x + 1?, most compilers (good) optimize and do the exact same thing.
Compiler that I am using - gcc version 5.4.0