Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Sun Nov 09, 2014 8:48 am
by Nisim123
Looking at the code at the first sight:
public class DaysTest{
static String[] days = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
public static void main(String[] args) {
int index = 0;
for(String day : days){
if(index == 3){
break;
}else {
continue;
} // End of if-else statement.
index++;
if(days[index].length()>3){
days[index] = day.substring(0,3);
} // End of if(days[index].length()>3){ ........
}// End of for(String day : days){ ......
System.out.println(days[index]);
} // End of main
}// End of DaysTest class
I was innocently misled by the thought that the scope of the break
and the continue statements, since there are no labels, is limited to the if-else block only, and not for the entire enhanced for loop...
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Wed Nov 12, 2014 6:15 am
by Nisim123
OK so in a second thought after thinking about it for a while yesterday,
i've realized what was really bothering me in that question,
and it is the usage of break and continue statements in an if-else block,
which is forbidden in java. But it is ok to use break and continue inside an if-else block
when the block is in a for loop. Then i looked at the jls
where it discusses those issues (14.15 & 15.16), where it says almost the same
about those two statements, except for their actual action of course,
and that the continue statement can not be used in a switch block.
anyhow i've decided to quote here the relevant lines from the jls:
14.15 The break Statement
A break statement transfers control out of an enclosing statement.
BreakStatement:
break Identifier (opt) ;
A break statement with no label attempts to transfer control to the innermost
enclosing switch, while, do, or for statement of the immediately enclosing
method or initializer; this statement, which is called the break target, then
immediately completes normally.
To be precise, a break statement with no label always completes abruptly, the
reason being a break with no label.
If no switch, while, do, or for statement in the immediately enclosing method,
constructor, or initializer contains the break statement, a compile-time error
occurs.
and this one is about the continue statement:
A continue statement with no label attempts to transfer control to the innermost
enclosing while, do, or for statement of the immediately enclosing method,
constructor, or initializer; this statement, which is called the continue target, then
immediately ends the current iteration and begins a new one.
To be precise, such a continue statement always completes abruptly, the reason
being a continue with no label.
If no while, do, or for statement of the immediately enclosing method, constructor,
or initializer contains the continue statement, a compile-time error occurs.

Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Mon Jun 15, 2015 4:14 pm
by penguinLiason
Why wouldn't control go back to the enhanced for loop and continue the next iteration? That's where I got hung up...
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Mon Jun 15, 2015 8:32 pm
by admin
penguinLiason wrote:Why wouldn't control go back to the enhanced for loop and continue the next iteration? That's where I got hung up...
At what point in the code are you expecting this? If you execute break; in a for loop, the next iteration will not execute.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Fri Aug 21, 2015 10:31 am
by cmharden
So the "error: unreachable statement" compiler error only applies to for loops? The same logic in a while loop creates a indefinite loop...
int x,y = 0;
while(y<3){
if(x ==3){
continue;
}else{
break;
}
x++;
y++;
}
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Fri Aug 21, 2015 8:57 pm
by admin
Whenever a compiler can figure out that a line of code can never be executed, it will flag an error. It is not just about a loop. But remember that a compiler does not execute code. It can only consider the values that are compile time constants to determine potential execution path of a code.
I am not sure what you are trying to prove with the code that you've posted.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Sat Aug 05, 2017 8:08 am
by Kevin30
Hi,
To simplify the code:
Code: Select all
public class TestClass{
static String[] days = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
public static void main(String[] args) {
int index = 3;
for(String day : days){
if(index == 3){
continue;
}
else {
break;
}
System.out.println("Hello");
}
}
}
If I run this code, then I will get a compiler error because "System.out.println("Hello");" is an unreachable statement. I understand that.
What I don't understand, is that if I delete the else-statement in the if-else the code runs fine:
Code: Select all
public class TestClass{
static String[] days = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
public static void main(String[] args) {
int index = 3;
for(String day : days){
if(index == 3){
continue;
}
System.out.println("Hello");
}
}
}
Here, "System.out.println("Hello");" is an unreachable statement, but the code apparently executes fine anyway and I get no compiler error.
Can you help me explain this?
Thank you in advance!
Kevin
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Tue Aug 08, 2017 12:25 am
by admin
Compiler cannot execute the code if(index == 3). So it cannot know whether this condition will be true or not at runtime because index is not a compile time constant. Thus, it has no option but to let the code compile. This is the general idea.
You may now expect that if index were declared final, it would fail to compile but that is not true either.
There is one more rule applicable for the if statement. The if statement has been explicitly exempted from the rule about unreachable statements to support conditional compilation.
So while the following will not compile:
while (false) { x=3; }
the following will:
if (false) { x=3; }
The exact details are described here:
https://docs.oracle.com/javase/specs/jl ... #jls-14.21
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Fri Mar 16, 2018 9:36 am
by Rick84
In this question the answer would have been 'monday' if the unreachable code wasn't there. I only realized that the compiler would be able to figure out that the code was unreachable after I noticed 'monday' wasn't a possible answer.
I noticed this with more questions. The expected answer isn't available, which causes me to look closer and come to the right conclusion. Is this something I can expect of the actual exam as well?
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Fri Mar 16, 2018 10:14 am
by admin
It is possible but probably not done on purpose. I think it is most probably due to oversight by the author of the question.
Yes, you may expect such situation in the real exam as well where it is easy to guess the answer by the process of elimination.
I have updated the question.
thanks!
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Wed May 09, 2018 12:28 pm
by elias86
Hi all!
I don't understand why
index++;
Is not reacheable code. Can someone help me to understand please?
Thanks, Elias.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Wed May 09, 2018 9:25 pm
by admin
Try to execute the code mentally and see if you can think of a situation when this line of code will be executed.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Sun Jun 24, 2018 7:48 am
by flex567
Is the break/continue statement allowed in the for-each loop ?
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Sun Jun 24, 2018 8:12 am
by admin
What happened when you tried it out?
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Sun Jun 24, 2018 5:23 pm
by flex567
It is allowed.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Mon Jan 07, 2019 2:58 pm
by crazymind
Hi, I know compiler doesn't execute any code but how does compiler know that code is unreachable without executing it. It seems quite impossible to me.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Mon Jan 07, 2019 10:06 pm
by admin
Compiler can look for things that are evident from the code at compile time and draws inferences from them. For example, if a variable is declared final then the compiler can draw the inference that the variable will always have the same value at run time. It doesn't need to execute the code to know the value of the variable. Based on that it can find out if some part of code is unreachable. There are many such things.
This is not required for the exam but if you want to learn more, you will need to go through a book on Compiler Design to understand what all can a compiler do and how it does it.
Re: About Question enthuware.ocajp.i.v7.2.1411 :
Posted: Tue Jan 08, 2019 12:25 am
by crazymind
ok, thanks