About Question enthuware.ocajp.i.v7.2.1090 :
Posted: Wed Feb 04, 2015 1:25 pm
Hi Enthuware Admin and everybody.
I don't understand this answer "It will print Sum = 0".
Ok, variable sum is never changed but... isn't this a case of "unreachable code"?
int sum = 0;
for (int i = 0, j = 10; sum > 20; ++i, --j) // 1
{
sum = sum+ i + j;
}
At compile time sum = 0 and the condition to enter the for loop (sum>20) is not satisfied at the first iteration.
So the instruction sum = sum+ i + j; can never be executed.
Maybe compiler doesn't check the for loop statements or it doesn't check the variables value?
I'm confused, can someone help me?
Thank you so much.
G.
I don't understand this answer "It will print Sum = 0".
Ok, variable sum is never changed but... isn't this a case of "unreachable code"?
int sum = 0;
for (int i = 0, j = 10; sum > 20; ++i, --j) // 1
{
sum = sum+ i + j;
}
At compile time sum = 0 and the condition to enter the for loop (sum>20) is not satisfied at the first iteration.
So the instruction sum = sum+ i + j; can never be executed.
Maybe compiler doesn't check the for loop statements or it doesn't check the variables value?
I'm confused, can someone help me?
Thank you so much.
G.