Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.15" rel="nofollow">Java Language Specification section on the <code>break</code> statement</a>:</p> <blockquote> <p>A break statement transfers control out of an enclosing statement.</p> </blockquote> <p>and...</p> <blockquote> <p>a break statement ... always completes abruptly</p> </blockquote> <p>a) It simply allows transfer of control out of the case statement. </p> <p>b) The reason why it executes the conditions that aren't what you would expect to be matching is because there are particular conditions where falling through case statements would be considered valid (Taken from <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html" rel="nofollow">The Java Tutorials</a>):</p> <pre><code>List&lt;String&gt; futureMonths = new ArrayList&lt;String&gt;(); int month = 8; switch (month) { case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); break; default: break; } for (String monthName : futureMonths) { System.out.println(monthName); } </code></pre> <p>Which outputs:</p> <blockquote> <p>August </p> <p>September </p> <p>October </p> <p>November </p> <p>December</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload