Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

break using label

 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

out:
?0
1
?1
2
2
?2
3
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);".
why?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Corey
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx, Corey!
I got it.
"break label" causes do Loop to be abandoned.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Val Lee
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx,srinivas!
Good Job!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic