Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Sat Sep 01, 2012 8:27 pm
by ETS User
The "Jill:" label doesn't surround a iteration loop or a Brace-enclosed code block. How would this
label be used. I know it compiles successfully, but with warnings.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Sun Sep 02, 2012 6:47 am
by admin
I just tried compiling the code. There is no warning. It is true that the label makes no sense but it is legal.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Tue Jan 22, 2013 12:15 am
by icepeanuts
Yes, it compiles. But I also remember some books said the labels should be used with blocks (e.g., For loop and code blocks). Just feel very confused.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Tue Jan 22, 2013 7:01 am
by admin
You can use label at almost any line of code.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Wed Jan 23, 2013 12:10 am
by icepeanuts
I just got the idea. The label can be used with any line of code. But how you use it can make the code valid or invalid. Take, an example,
int c = 0;
JACK: while (c < 8) {
JILL: System.out.println(c);
if (c > 3)
break JACK;
else
c++;
}
This block of code has no problems.
However, if the code is changed like this, it becomes invalid because of break JILL;
int x = 0;
JACK: while (x < 8) {
JILL: System.out.println(x);
if (x > 3)
break JILL;
else
x++;
}
In this case, the scope of JILL extends only up till System.out.println(x); and break JILL; is out of the scope of the label.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Wed Jan 23, 2013 6:27 am
by admin
That is correct

Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Thu Feb 28, 2013 12:57 pm
by Guest
It seems a label can't be placed where it is envolved a declaration as follows:
MYLABEL:
int a=0;
Best regards
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Fri Mar 01, 2013 7:08 am
by admin
Well, yes, any where is not really literally anywhere. You can't put it in front of class declaration either MYLABEL: public class Test { }. I have updated the post to avoid confusion.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Mon Nov 25, 2013 12:40 pm
by Zoryanat
I refer to explanation that is given to this question - I pasted it below for your convenience. In example given first, where do they come up with print out "c=0"? I mean, I can see only println(c) - e.g. there is no println("c = "+c), unless I am missing something. The same goes for "c=1", "c=2", etc. (highlighted in blue below).
Secondly, when JILL loop runs for first time, c is 0, k is 0 too, so how can it even enter executing statements in for loop (int k=0; k<c; k++)?
I must be missing something very obvious here, please help me to understand what.
Regards
Zoryana
-----------
"This is a straight forward loop that contains a labelled break statement. A labelled break breaks out of the loop that is marked with the given label. Therefore, a labelled break is used to break out from deeply nested loops to the outer loops. Here, there is only one nested loop so the break; and break JACK; are same, but consider the following code:
public static void crazyLoop(){
int c = 0;
JACK: while (c < 8){
JILL: System.out.println(c);
for(int k = 0; k<c; k++){
System.out.println(" k = "+k+" c = "+c);
if (c > 3) break JACK;
}
c++;
}
}
This code prints:
c = 0
c = 1
k = 0 c = 1
c = 2
k = 0 c = 2
k = 1 c = 2
c = 3
k = 0 c = 3
k = 1 c = 3
k = 2 c = 3
c = 4
k = 0 c = 4
As you can see, in this case, break JACK; will break out from the outer most loop (the while loop). If break JACK; is replaced by break; it will print:
c = 0
c = 1
k = 0 c = 1
c = 2
k = 0 c = 2
k = 1 c = 2
c = 3
k = 0 c = 3
k = 1 c = 3
k = 2 c = 3
c = 4
k = 0 c = 4
c = 5
k = 0 c = 5
c = 6
k = 0 c = 6
c = 7
k = 0 c = 7
This shows that a break without a label only breaks out of the current loop."
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Mon Nov 25, 2013 1:57 pm
by admin
1. You are right, the code should have println("c = "+c) to match the output.
2. In the first interation of the outer loop, c is 0. So the inner loop with k doesn't execute. The output shows that. (There is nothing between the first two lines:
c = 0
----no output here
c = 1
). THe innerloop executes once c becomes 1. So I am not sure what is your doubt?
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
Posted: Tue Nov 26, 2013 4:59 am
by Zoryanat
LOL
Yes. Somehow today it's making perfect sense. Thanks for breaking it down for me, Paul
Rds
Zoryana