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.
About Question enthuware.ocajp.i.v7.2.1090 :
Moderator: admin
-
- Posts: 7
- Joined: Wed Feb 04, 2015 1:06 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1090 :
Compiler doesn't execute code so it can't evaluate variables. I can only look at compile time constants to see if some code is unreachable or not. That is why there is no compilation error for unreachable code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Wed Feb 04, 2015 1:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1090 :
Thank you.
It's clear now.
It's clear now.
Who is online
Users browsing this forum: No registered users and 88 guests