Switch Case Fall Through Scenario

8.2k Views Asked by At

So I came upon a competitive question (asking the output) as follows:

#include <stdio.h>
int main()
{
    int i = 0;
    for(i = 0; i < 20; i++)
    {
        switch(i)
        {
            case 0: i+=5;
            case 1: i+=2;
            case 5: i+=5;
            default: i+= 4;
            break;
        }
        printf("%d ", i);
    }
    return 0;
}

The output is 16, 21. While I know how switch case works I am unable to explain myself how this fall through works. Why is the default getting added? Doesn't K&R C book say that the default only executes if none of cases match?
Thanks.

4

There are 4 best solutions below

1
On BEST ANSWER

The default case is only jumped to from the switch statement if no other cases match. After one of them has matched, the code executes as if none of the case statements existed, unless it hits a break. So the default case isn't "jumped over" the way you seem to expect.

K & R is a little bit unclear about this, the line you're referring to seems to be:

If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied

But this is talking about how the switch statement branches. Fallthrough behavior is on the next page:

Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape.

which doesn't depend on whether or not there's a default case.

The C standard is clearer:

A switch statement causes control to jump to, into, or past the statement that is the switch body , depending on the value of a controlling expression ... If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement.

Once control jumps, the case and default labels don't matter anymore.

0
On

There is no "break" included in the switch cases other than default.

When a switch statement is used without breaks, the code continues executing even after a matching case is found.

Please see below the corrected code :

    switch(i)
    {
        case 0: i+=5;
        break;
        case 1: i+=2;
        break;
        case 5: i+=5;
        break;
        default: i+= 4;
        break;
    }
0
On

You need to add break statement for every time you need to end that particular case or else it is going to fallthrough all case statements

0
On

The "body" of switch statement is one contiguous linear compound statement. The case labels are just that - labels, which define different entry points into that contiguous statement. The default label is also just a label, no different from case labels in that regard.

Once you entered into the switch compound statement through a label, the execution will continue in a normal fashion: sequentially all the way to the end of the compound statement (at that stage case/default labels no longer play any role). If you want to prevent this from happening, it is your responsibility to jump out of that compound statement at the appropriate moment. You can use any jump statement for that, keeping in mind that you also have break at your disposal (which is in most cases the most appropriate choice).

In other words, switch is not a highly structured branch selector, as it might appear at the first sight. switch is just a slightly structured multi-targeted goto. All it does is a single run-time parametrised jump. Everything after that jump is your responsibility.

In your code sample you placed a lone break at the very end of switch compound statement. In that position it actually achieved absolutely nothing, since the compound statement ends there anyway.