About Question enthuware.ocajp.i.v7.2.1281 :
Moderator: admin
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1281 :
Sorry I dint get your explanation on answer to this question can you explain it with code examples?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
This link explains break and continue is very clearly: https://docs.oracle.com/javase/tutorial ... ranch.html
Please read the above and then the explanation will make more sense.
Please read the above and then the explanation will make more sense.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
hi,
just thought i'd add a little comment:
your answer RE labelled breaks and continues says that 'the break target need not be a while, do, for, or switch statement', which suggests that a break label can be placed anywhere, which doesn't seem to be true.
the link to the oracle tutorials doesn't appear to be clear on exactly where the label can be placed either.
to be honest, it doesn't really matter - i can't imagine the exam placing the label in an incorrect position, but i am interested, as i've been getting some strange results
thanks
nick
just thought i'd add a little comment:
your answer RE labelled breaks and continues says that 'the break target need not be a while, do, for, or switch statement', which suggests that a break label can be placed anywhere, which doesn't seem to be true.
the link to the oracle tutorials doesn't appear to be clear on exactly where the label can be placed either.
to be honest, it doesn't really matter - i can't imagine the exam placing the label in an incorrect position, but i am interested, as i've been getting some strange results
thanks
nick
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
A break without a label can be used anywhere to break a scope. The following work:NickWoodward wrote:hi,
just thought i'd add a little comment:
your answer RE labelled breaks and continues says that 'the break target need not be a while, do, for, or switch statement', which suggests that a break label can be placed anywhere, which doesn't seem to be true.
Of course, you can't do:int x = 0;
stmt: if(x==2){
break stmt; //breaking if
}
stmt1: {
System.out.println("test");
break stmt1; //breaking the innermost scope
}
For more details you may refer to the JLS.stmt: System.out.println("test");
break stmt1; //invalid because there is no scope to break.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
ahh, that makes sense, thanks!
I was wondering why this worked:
but this wouldn't:
now I understand that the label must at least be next to a block of some kind.
thanks for the explanation!
nick
I was wondering why this worked:
Code: Select all
for(;;){
int x = 0;
lab:
if(x==0){
System.out.println();
break lab;
}
}
Code: Select all
for(;;){
lab: // if brace put here and after if statement code will compile
int x = 0;
if(x==0){
System.out.println();
break lab;
}
}
thanks for the explanation!
nick
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
Please give an example of the last sentence (underlined) of the following. Thanks
"A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. The continue target must be a while, do, or for statement or a compile-time error occurs. If no labeled statement with Identifier as its label contains the continue statement, a compile-time error occurs."
"A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. The continue target must be a while, do, or for statement or a compile-time error occurs. If no labeled statement with Identifier as its label contains the continue statement, a compile-time error occurs."
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
Code: Select all
MYLABEL: System.out.println("before loop");
for(int i = 0; i<10; i++) //label MYLABEL should have been on this line
{
if(i == 5) continue MYLABEL; //compilation error because continue target is not the containing loop
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Mon Jul 20, 2020 1:30 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
the wording "break without a label, *can occur only* in a switch, while, do, or for statement." would imply inversely that you cannot have a break without label in anything but a switch, while, do or for statement, which then excludes if statements. Obviously break without label can be used within a conditional statement. Just seemed unclear to me. Maybe better wording would be "break without a label, must be surrounded by a switch, while, do, or for statement. Or maybe ...must be within a ... or am I totally off?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
>Obviously break without label can be used within a conditional statement.
Have you tried using a break without a label in an if statement?
Have you tried using a break without a label in an if statement?
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Jun 03, 2022 9:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1281 :
"break without a label, can occur only in a switch, while, do, or for statement."
Obviously you have the option of having break with label associated with outer for and break without label associated with inner for in the code. Doesn't really make sense to me.
Obviously you have the option of having break with label associated with outer for and break without label associated with inner for in the code. Doesn't really make sense to me.
Who is online
Users browsing this forum: Bing [Bot] and 108 guests