What does the break command do in java?

I get how the break command does things, but I a little bit more explanation in depth.
Link to lesson where the break command is first used : https://www.codecademy.com/courses/learn-java/lessons/java-conditionals-and-control-flow/exercises/switch

1 Like

What part about it needs more explanation specifically? In terms of use case, you can use it inside of a loop as a reaction to some condition being met.
This saves runtime since you don’t have to loop through your entire list (which can be potentially huge). It might also be that your code inside the loop is specifically written to work for a specific subset of values, and so breaking the loop when the subset finished also potentially helps your code run as you intend for it to.

It always helps to look at documentation when wanting a more in-depth look: Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

Hey! I was wondering if it just divided the code into multiple parts or did it just prevent it from looping through my entire list.

It stops the loop. Great for when all conditions of what you’re looping for are met.

1 Like