permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes break using label Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "break using label" Watch "break using label" New topic

break using label

Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41

When x is 1,it get "break label" in switch().
Next,I think it should be perform "x++".
But computer shows it performs "System.out.println("?" + x);".

Val SCJP2 1.2<br />====>SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
From the JLS, §14.14 The break Statement:

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (�14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally.

So, you're right when you say that the target is the do loop. However, that do loop ends abruptly and we continue with the next iteration of the outer loop.
That's why we end up flowing directly into the next iteration of the outer loop.
I hope that helps,

SCJP Tipline, etc.
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Thx, Corey!
I got it.
"break label" causes do Loop to be abandoned.
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi val, x++ will get executed when you have continue there instead of break.,break statement takes you out of the loop,higher up to the next label which is in higher order., i have changed ur code little bit here ... see this...HTH

please use the [code][/code] tags when showing code. visit <a href="" target="_blank" rel="nofollow"></a> ,for more details
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Good Job!
I agree. Here's the link:
subject: break using label
It's not a secret anymore!