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

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

Moderator: admin

Post Reply
Zoryanat
Posts: 20
Joined: Tue Aug 27, 2013 3:16 am
Contact:

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

Post by Zoryanat »

Could you please help me to understand what is correct syntax to enclose all three lines into scope of Jill label:

JACK: while (c < 8){
JILL: System.out.println(c);
if (c > 3) break JILL; else c++;
}

Many thanks!
Regards
Zoryana

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

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

Post by admin »

You can put it in a block like this:
JACK: while (c < 8){

JILL: {
System.out.println(c);
if (c > 3) break JILL; else c++;
}

}
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

I used this code
void crazyLoop(){
int c = 0;
JACK: while (c < 8){
JILL: {
System.out.println(c);
if (c > 3) break JILL; else c++;
}}}
And it keep print 4 forever.
Can we use break label in this manner practical?

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

Can we put break clause inside a block of code {...} that is not within a loop-statement nor switch?

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

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

Post by admin »

Try it out :)
If you like our products and services, please help us by posting your review here.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

Thanks :D
I know that that is the best way :)

Well, if we use unlabeled break we can put it only inside the loops or switch statements. Otherwise it wont compile and we get a message:

Code: Select all

error: break outside switch or loop

But if we use labeled break, we can put it also inside of ordinary block of code

Code: Select all

myLabel: { //some code

if (condition)
   break myLabel;

// some code
}
And both, labeled and unlabeled continue can be used only inside the loops.

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

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

Post by Rick84 »

I understand that you can add a label to any statement that isn't a declaration, but you can only use break in blocks and continue in loops. So am I right in saying that the JILL label in the question is comepletely useless? Or am I missing something? :)

Code: Select all

void crazyLoop(){
   int c = 0;
   JACK: while (c < 8){
       JILL: System.out.println(c);
       if (c > 3) break JILL; else c++;
   }
}

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

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

Post by admin »

Yes, it is pretty much useless.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 63 guests