Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to use break to exit a loop in Java? [closed]

I was wondering if it is a "bad practice" to use a break statement to exit a loop instead of fulfilling the loop condition?

I do not have enough insight in Java and the JVM to know how a loop is handled, so I was wondering if I was overlooking something critical by doing so.

The focus of this question: is there a specific performance overhead?

like image 699
Don Avatar asked Aug 12 '13 13:08

Don


People also ask

Is it bad practice to use break in Java?

Break and continue are OK unless you have several of them in the loop which becomes confusing.

Can you exit a loop with break?

break can only exit out of an enclosing loop or an enclosing switch statement (same idea as an enclosing loop, but it's a switch statement). If a break statement appears in an if body, just ignore the if.

Does Break take you out of a for loop Java?

Generally break statement breaks out of loops ( for , while , and do... while ) and switch statements.


1 Answers

Good lord no. Sometimes there is a possibility that something can occur in the loop that satisfies the overall requirement, without satisfying the logical loop condition. In that case, break is used, to stop you cycling around a loop pointlessly.

Example

String item;  for(int x = 0; x < 10; x++) {     // Linear search.     if(array[x].equals("Item I am looking for"))     {        //you've found the item. Let's stop.        item = array[x];        break;      } } 

What makes more sense in this example. Continue looping to 10 every time, even after you've found it, or loop until you find the item and stop? Or to put it into real world terms; when you find your keys, do you keep looking?

Edit in response to comment

Why not set x to 11 to break the loop? It's pointless. We've got break! Unless your code is making the assumption that x is definitely larger than 10 later on (and it probably shouldn't be) then you're fine just using break.

Edit for the sake of completeness

There are definitely other ways to simulate break. For example, adding extra logic to your termination condition in your loop. Saying that it is either loop pointlessly or use break isn't fair. As pointed out, a while loop can often achieve similar functionality. For example, following the above example..

while(x < 10 && item == null) {     if(array[x].equals("Item I am looking for"))     {         item = array[x];     }      x++; } 

Using break simply means you can achieve this functionality with a for loop. It also means you don't have to keep adding in conditions into your termination logic, whenever you want the loop to behave differently. For example.

for(int x = 0; x < 10; x++) {    if(array[x].equals("Something that will make me want to cancel"))    {        break;    }    else if(array[x].equals("Something else that will make me want to cancel"))    {        break;    }    else if(array[x].equals("This is what I want"))    {        item = array[x];    } } 

Rather than a while loop with a termination condition that looks like this:

while(x < 10 && !array[x].equals("Something that will make me want to cancel") &&                  !array[x].equals("Something else that will make me want to cancel")) 
like image 189
christopher Avatar answered Oct 11 '22 08:10

christopher