Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: When coding, why is exiting a loop using break; bad?

  1. Feb 19, 2008 #1
    When coding, why is exiting a loop using"break;" bad?

    when coding, to exit a loop i was told using "break;" (in java) is really bad...why!
  2. jcsd
  3. Feb 19, 2008 #2
    It makes the code hard to follow. If the exit criteria is at the top of the loop, it is easy for the reader to figure out what is happening. However a break buried deep in a loop can do something surprising and surprises make code very difficult to follow. Then there are labeled breaks which act somewhat the same as a goto in C (more or less) and can quickly turn your code into an unreadable mess.

    Breaks are always ok in switch statements. But elsewhere USE CAUTION!!!!

    Remember any significant chunk of code will be maintained by someone else and that someone will want easy to read code not one riddled with hidden break statements.
    Last edited: Feb 19, 2008
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook