Page 1 of 1

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

Posted: Mon Nov 25, 2013 8:05 pm
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

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

Posted: Tue Nov 26, 2013 7:28 am
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++;
}

}

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

Posted: Tue Apr 01, 2014 1:01 pm
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?

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

Posted: Fri Jan 02, 2015 12:29 am
by coder007
Can we put break clause inside a block of code {...} that is not within a loop-statement nor switch?

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

Posted: Fri Jan 02, 2015 1:50 am
by admin
Try it out :)

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

Posted: Sat Jan 03, 2015 3:37 pm
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.

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

Posted: Tue Mar 13, 2018 7:34 am
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++;
   }
}

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

Posted: Tue Mar 13, 2018 7:43 am
by admin
Yes, it is pretty much useless.