This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Control Flow: Switch Forum

View Course » View Exercise

142 points
8a2c420b2188e3b007a7cbdbc6cecb0e?s=140&d=retro
Submitted by
Rijul Gulati
about 2 years ago

The break statement

Hello,
Here is a piece of code I wrote.

    <!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
        $num=5;
        switch($num) {
            case 0:
                echo "Number = 0";
                break;
            case 1:
                echo "Number =1";
                break;
            case 2: 
            case 3:
            case 4: 
            case 5:
                echo "Number lies between 2 and 5";

            case 6:
                echo "Number = 6";

            case 7:
            case 8:
            case 9:
            case 10:
                echo "Number lies between 7 and 10";

            default: 
                echo "Invalid input";
                break;
        }
    ?>
    </body>
</html>

My doubt: I Intentionally removed the break statement after "case 2,3,4,5" , "case 6" and "case 7,8,9,10".
The break statement prevents the code to jump to next case automatically if a match is found. Right?
After removing the above mentioned break(s) statement, the output obtained is "Number lies b/w 2 and 5 Number = 6 Number lies b/w 7 and 10".
I understand that because of absence of break, the code continues the check next case, even if match is found.
But, how come the other cases successfully match, even if they actually don't?
The output should have been "Number lies b/w 2 and 5" only. Isn't it?
Help please.


1 vote

permalink

Hi
Let me explain it case by case:
case 0 ------> Has code to execute. Has the break statement. OK
case 1 ------> Has code to execute. Has the break statement. OK
case 2 ------> No code to execute. No need for the break statement. OK
case 3 ------> No code to execute. No need for the break statement. OK
case 4 ------> No code to execute. No need for the break statement. OK
case 5 ------> Matches the $num value. Has code to execute, so it does. oops! there is no
break statement. so the switch still runs.
case 6 ------> Has code to execute, so it does because the $num value has matched with a
case earlier. And there is no break statement so the switch still runs.
case 7 ------> No code to execute. No need for the break statement. OK
case 8 ------> No code to execute. No need for the break statement. OK
case 9 ------> No code to execute. No need for the break statement. OK
case 10 ------> Has code to execute, so it does because the $num value has matched with a case earlier. And there is no break statement so the switch still runs.
default ------> Has code to execute, but it doesn't. This happens because the $num value has matched with a case earlier. It would have been executed if the $num value didn't match any cases.
I hope you understand!

530 points
55ad5d19937676e49500008f_877399053
Submitted by
Amirata
about 2 years ago