About Question enthuware.ocajp.i.v7.2.1057 :
Moderator: admin
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1057 :
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
JACK: while (c < 8){
JILL: System.out.println(c);
if (c > 3) break JILL; else c++;
}
Many thanks!
Regards
Zoryana
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
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++;
}
}
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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
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?
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?
-
- Posts: 25
- Joined: Wed Dec 17, 2014 9:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
Can we put break clause inside a block of code {...} that is not within a loop-statement nor switch?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
Try it out
If you like our products and services, please help us by posting your review here.
-
- Posts: 25
- Joined: Wed Dec 17, 2014 9:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
Thanks
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:
But if we use labeled break, we can put it also inside of ordinary block of code
And both, labeled and unlabeled continue can be used only inside the loops.
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
}
-
- Posts: 12
- Joined: Tue Mar 06, 2018 9:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
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++;
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1057 :
Yes, it is pretty much useless.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 63 guests