1

Here's the code:

#include <stdio.h>

int main ()
{
    int menu=0;
    while (menu!=3)
    {
        scanf ("%d", &menu);
        switch (menu)
        {
            case 1:
            {
                printf ("Case 1\n");
                continue;
            }

            case 2:
            {
                printf ("Case 2\n");
                break;
            }
        }
        printf ("This doesn't get printed by case 1\n");
    }

    return 0;
}

Every time I put it 1, that printf won't show up, but other than that, it works well. So how continue and break works for switch inside a loop? Why break doesn't break the loop? Instead, it prevents the next cases to be done right? But what about continue? To be clear, I'm actually asking how is it different and how those actually work. Note: I've done this too in java (with eclipse).

Artyer
  • 31,034
  • 3
  • 47
  • 75
  • 11
    Because `break` in a switch has a specific meaning (end the current case), but `continue` doesn't (so it means the same as if it were outside the switch). – Andy Turner Apr 23 '20 at 15:18
  • Does this answer your question? [Java How can I break a while loop under a switch statement?](https://stackoverflow.com/questions/22823395/java-how-can-i-break-a-while-loop-under-a-switch-statement) – Savior Apr 23 '20 at 15:19
  • @Savior Unfortunately, no, I think it's different from my question which is asking the usage, why is it different, the cause as I stated in this post's body. – LastSecond959 Apr 23 '20 at 15:26
  • @AndyTurner So you mean, break in a case is some kind of special and works like "breaking the case and go outside the switch", while continue doesn't, so it only affects the loop like usual continue? – LastSecond959 Apr 23 '20 at 15:28
  • 1
    If you don't use `break` in a switch it falls through to the next case label. We use `break` to exit the entire switch statement. – Blastfurnace Apr 23 '20 at 15:39

2 Answers2

2

B (and thereafter its descendants including C++, Java or JavaScript) overloads the break keyword to jump out of switches.

So when you're in a nested loop/switch situation, the break keyword will apply to the innermost switch or loop, whichever is closer.

This overload is an accident of history. B started by copying BCPL and while BCPL later gained an endcase keyword design specifically for breaking switches, Thompson and Ritchie were not aware of the change and so B and C remained with their home-made readaptation of break for this purpose.

( http://port70.net/~nsz/c/c89/dmr_the_development_of_the_c_language.pdf

Not every difference between the BCPL language documented in Richards’s book [Richards79] and B was deliberate; we started from an earlier version of BCPL [Richards 67]. For example, the endcase that escapes from a BCPL switchon statement was not present in the language when we learned it in the 1960s, and so the overloading of the break keyword to escape from the B and C switch statement owes to divergent evolution rather than conscious change.

)

In any case, in B/C/C++, break/continue is nothing but syntactic sugar for a goto and if you want to break/continue something other than the innermost loop/switch, you can always achieve it by using an explicit goto:

#include <stdio.h>

int main ()
{
    int menu=0;
    while (menu!=3)
    {
        scanf ("%d", &menu);
        switch (menu)
        {
            case 1:
            {
                printf ("Case 1\n");
                continue;
            }

            case 2:
            {
                printf ("Case 2\n");
                goto break_loop;
                //`return 0;` would work too in this case
                break; //would break out of the switch

            }
        }
        printf ("This doesn't get printed by case 1\n");
    }
break_loop:
    return 0;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

continue specifically means that the processing in the current loop is skipped and the next iteration of the loop is started (assuming that the conditions for the loop to run still stand).

In your code there's only one loop.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • So, `break` in a case is "programmed" to break the `switch`, not the loop right? While `continue` is like usual continue? – LastSecond959 Apr 23 '20 at 15:33
  • 1
    @LastSecond959 There's nothing to `continue` in a `switch` because `switch`s don't loop. Or from another perspective, you can think of a `continue` in a loop as a `goto right_before_the_closing_right_brace;` and a `break` as a `goto right_after_the_closing_brace`, but in a `switch` those two have the same effect and forcing `continue` to also apply to `switch`es would only serve to decrease the expressiveness of the language. – Petr Skocik Apr 23 '20 at 15:50