About Question enthuware.ocajp.i.v7.2.1121 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1121 :
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.
label be used. I know it compiles successfully, but with warnings.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
You can use label at almost any line of code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
That is correct
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1121 :
It seems a label can't be placed where it is envolved a declaration as follows:
MYLABEL:
int a=0;
Best regards
MYLABEL:
int a=0;
Best regards
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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."
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."
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1121 :
LOL
Yes. Somehow today it's making perfect sense. Thanks for breaking it down for me, Paul
Rds
Zoryana
Yes. Somehow today it's making perfect sense. Thanks for breaking it down for me, Paul
Rds
Zoryana
Who is online
Users browsing this forum: No registered users and 92 guests