switch statemt functioning improperly and giving error when i put break;
<p>The following is the code i used in a program -
<strong>over here the month variable is an integer</strong></p>

<pre><code>  switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
  return 31;
  break;
case 2:
  return 28;
  break;
case 4:
case 6:
case 9:
case 11:
        return 30;
  break;
default:
  System.out.println("Invalid month.");
  return 0;
}
</code></pre>

<p><em>surprisingly, when i use the above switch construct.. it gives an error saying.. code unreachable for statements after each break statement</em></p>

<p>Then i removed all the break statements, and the new code looks like this ---</p>

<pre><code>  switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
  return 31;

case 2:
  return 28;

case 4:
case 6:
case 9:
case 11:
        return 30;

default:
  System.out.println("Invalid month.");
  return 0;
}
</code></pre>

<p>Now.. after removing the break statements .. the code worked perfectly well..</p>

<p>My question is... in the switch construct.. it is mandatory to use break.. or else the control flow will be continued.. and all the conditions are tested and executed!! right???</p>

<p>So why in the world is the previous  ** syntactically Right** version giving an error.. and the modified <strong>syntactically incorrect</strong> version running perfectly well..</p>

<p>Any explanation.. anyone!!</p>
