Page 1 of 1

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

Posted: Fri Nov 07, 2014 1:35 pm
by gparLondon
Sorry I dint get your explanation on answer to this question can you explain it with code examples?

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

Posted: Sun Nov 09, 2014 10:37 pm
by admin
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.

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

Posted: Thu Feb 11, 2016 11:17 am
by NickWoodward
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

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

Posted: Thu Feb 11, 2016 8:26 pm
by admin
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.
A break without a label can be used anywhere to break a scope. The following work:
int x = 0;
stmt: if(x==2){
break stmt; //breaking if
}

stmt1: {
System.out.println("test");
break stmt1; //breaking the innermost scope
}
Of course, you can't do:
stmt: System.out.println("test");
break stmt1; //invalid because there is no scope to break.
For more details you may refer to the JLS.

HTH,
Paul.

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

Posted: Fri Feb 12, 2016 7:16 am
by NickWoodward
ahh, that makes sense, thanks!

I was wondering why this worked:

Code: Select all

		
for(;;){
	int x = 0;
	lab:
	if(x==0){
		System.out.println();
		break lab;
	}
}
but this wouldn't:

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;
	}
}
now I understand that the label must at least be next to a block of some kind.

thanks for the explanation!

nick

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

Posted: Fri Nov 30, 2018 3:07 pm
by OCAJO1
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."

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

Posted: Fri Nov 30, 2018 9:59 pm
by admin

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
}

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

Posted: Mon Jul 20, 2020 9:11 pm
by admin
>Obviously break without label can be used within a conditional statement.
Have you tried using a break without a label in an if statement?

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

Posted: Sun Jun 05, 2022 7:33 pm
by herngyih
"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.