Page 1 of 1

Question about some book

Posted: Thu May 17, 2018 12:57 am
by flex567
In the book for the exam it is explicitly mentioned that if statement don't support break, but you don't mention that in the answer nor in that post.
I don't know who is right you or the book?
Capture.PNG
Capture.PNG (38.15 KiB) Viewed 2001 times

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

Posted: Thu May 17, 2018 1:54 am
by admin
Well, three things -
1. what happened when you compiled the code with a break in if?
Here is the code that compiles fine:

Code: Select all

void crazyLoop(){
   int c = 0;
   JACK: while (c < 8){
       System.out.println(c);
       if (c > 3) {
         break JACK;
       }
   }
} 
2. The table in the book is talking about breaking an if condition, (and not about breaking from an if contained in a labelled loop) which of course is not possible but you should check with the author.

3. This point is not covered in this question because the this question is about something else. But it is explained clearly in another question 2.1193. Each question will not cover all the points (because that is not the objective of the questions), but if you go through all the questions, you can be assured that all the points will be covered.

HTH,
Paul.

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

Posted: Thu May 17, 2018 3:53 pm
by flex567
But it is explained clearly in another question 2.1193.
I couldn't find the question enthuware.ocajp.i.v7.2.1193.

Are you refering to some other question?

From the book:
The break statement can take an optional label parameter.
Without a label parameter, the break statement will terminate the nearest inner loop it is
currently in the process of executing. The optional label parameter allows us to break out
of a higher level outer loop.
"Without a label parameter, the break statement will terminate the nearest inner loop it is
currently in the process of executing." -> "If" statement doesn't count as the inner loop?

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

Posted: Thu May 17, 2018 7:59 pm
by admin
1. How did you search? See attached image. This question is there in v7 as well as in v8.

2. You really need to ask the author of the book regarding statements from the book. But yes, "if" is not a loop. I think the book must have discussed this while talking about loops. So an unlabelled break in an if block is not valid unless the if statement is within a loop.
But a labelled break from an if block is possible. The following code compiles and runs fine :

Code: Select all

  public static void main(String[] args){
    int i  = 0;
     LABEL: if(true){
         System.out.println("break label");
         i = 3;
         if( i == 3){
           break LABEL; //this is valid
         }
         System.out.println("not executed");
      }
  } 

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

Posted: Fri May 18, 2018 4:57 pm
by flex567
I only searched with Ctrl+Q.

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

Posted: Fri May 18, 2018 10:31 pm
by admin
It opens with Ctrl Q as well. You need to enter just 2.1193
You probably entered enthuware.ocajp.i.v7.2.1093 instead of enthuware.ocajp.i.v8.2.1093.

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

Posted: Sat May 19, 2018 5:10 am
by flex567
yes I entered entered enthuware.ocajp.i.v7.2.1093