How many iterations of the following loop will be performed?
 for(int x = 0; x = 3; x++); 
Explanation
The assignment operator in C++ returns the value of the assignment to make chaining possible:
int a = b = c = 3;
After this assignment a, b, c will all be equal to 3. This line is also equal to
int a = (b = (c = 3));
In C++ assigning int to bool is made with implicit cast, and
bool b = (x = 3);
will result to 1. Thus the loop condition will always evaluate to true and we get infinite loop.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback