• Post Reply Bookmark Topic Watch Topic
  • New Topic

Break statement Question  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I want to make sure I understand this.   Oracle shows a break and a labeled break.  It says that a labeled break terminates an outer statement so I take it that a unlabeled break terminates the loop it is in?  Is that correct?
Here is where I find this information .  https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html




Here is the labeled break statement example.  Also the word "search" is it's "label" correct?   To use this do I always add it right before the first condition / loop?









 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your understanding is correct, except: an if is not a loop, so in the first example, it breaks out of the for loop, not the if statement.

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Your understanding is correct, except: an if is not a loop, so in the first example, it breaks out of the for loop, not the if statement.



Ah I think I get it.  So say the "if" was a "for".  THEN the Break would jump out of THAT "for" loop.

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caveat on using break statement: use it very carefully.  If you have multiple break + return statements or have a break that's deep inside a long (more than 10 lines of code) method and/or nested loop structure, the complexity of the code could increase dramatically.  Use of a break should always give you pause and you should consider whether there's a way to abstract away something to avoid using break. That said, if the surrounding code is small and the use of break is idiomatic such as exiting a potentially long-running loop, break can still be considered a prudent approach to improve performance.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Caveat on using break statement: use it very carefully. . . .
As Böhm and Jacopini showed in 1966, it is always possible to abstract break or similar away. A lot of people use break because you can get neater code in certain circumstances, but I agree with Junilu that you shou‍ld be very careful with it.

I also think that part of the Java™ Tutorials is badly written and they have some poor quality code to show break continue and labels.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:Ah I think I get it.  So say the "if" was a "for".  THEN the Break would jump out of THAT "for" loop.

Yes, correct!
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:
No, here when labeled break statement executed then control jumps out of both inner and outer for loop. Yes correct search: is called as a labeled statement.

Example:Output:
Demo 1:
012345
Demo 2:
I'm in for loop one 012345 I'm in for loop one 012345 I'm in for loop one 012345
Demo 3:
I'm in for loop one 012345 I'm in for loop one 012345 I'm in for loop one 012345
Demo 4:
I'm in for loop one 012345
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!