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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Sorry I dint get your explanation on answer to this question can you explain it with code examples?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

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

Post 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

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
}
If you like our products and services, please help us by posting your review here.

se.le7204
Posts: 2
Joined: Mon Jul 20, 2020 1:30 pm
Contact:

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

Post by se.le7204 »

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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

herngyih
Posts: 6
Joined: Fri Jun 03, 2022 9:14 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 29 guests