About Question enthuware.ocajp.i.v7.2.1411 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1411 :

Post 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...

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
;)

penguinLiason
Posts: 4
Joined: Tue May 26, 2015 2:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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...

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

cmharden
Posts: 1
Joined: Fri Aug 21, 2015 10:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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++;
}

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

Rick84
Posts: 12
Joined: Tue Mar 06, 2018 9:54 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post by elias86 »

Hi all!
I don't understand why

index++;

Is not reacheable code. Can someone help me to understand please?

Thanks, Elias.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post by flex567 »

Is the break/continue statement allowed in the for-each loop ?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post by flex567 »

It is allowed.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post 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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1411 :

Post by crazymind »

ok, thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests