62

Assuming this example code (source):

#include <stdio.h>

void playgame()
{
    printf( "Play game called" );
}
void loadgame()
{
    printf( "Load game called" );
}
void playmultiplayer()
{
    printf( "Play multiplayer game called" );
}

int main()
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            /* Note the colon, not a semicolon */
            playgame();
            break;
        case 2:
            loadgame();
            break;
        case 3:
            playmultiplayer();
            break;
        case 4:
            printf( "Thanks for playing!\n" );
            break;
        default:
            printf( "Bad input, quitting!\n" );
            break;
    }
    getchar();

    return 0;
}

should we use a break; in the last default case? If I remove it, I see the same behaviour of the program. However, I saw that other examples also use a break; in the default case.

Why? Is there a reason?

4
  • 14
    You don't need a break after any case label if it is the last one. Whether it is default or otherwise has nothing to do with that.
    – WhozCraig
    Commented Oct 1, 2014 at 10:19
  • Using the option -pedantic recent versions of gcc warn about a missing break after the last case.
    – alk
    Commented Oct 1, 2014 at 10:35
  • @alk I was warned by Eclipse, without this flag. Here is the compilation command: gcc -std=c99 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.c". Maybe -Wall was enough for that.
    – gsamaras
    Commented Oct 1, 2014 at 10:36
  • 1
    "Maybe -Wall was enough for that.": Even better! :-)
    – alk
    Commented Oct 1, 2014 at 14:41

7 Answers 7

117

Should we use a break; in the last default case?

From The C programming language - Second edition (K&R 2):

Chapter 3.4 Switch

As a matter of good form, put a break after the last case (the default here) even though it's logically unnecessary. Some day when another case gets added at the end, this bit of defensive programming will save you.

0
17

For one thing, you should think about why we should use break in switch statement. Look at this no-breaking switch statement.

switch ( input ) {
    case 1:            /* Note the colon, not a semicolon */
        playgame();
    case 2:
        loadgame();
    case 3:
        playmultiplayer();
    case 4:
        printf( "Thanks for playing!\n" );
    default:
        printf( "Bad input, quitting!\n" );
}

Suppose input == 1. The program will call playgame() of course, but since there's no break, program won't finish the switch but call loadgame(), playmultiplayer(), two printfs sequentially.

To avoid this, we use break.

case 1:
    playgame();
    break; /* here */
case 2:
    ...

Because of break, the program finishes switch statement before running codes of case 2. That's our expected result, isn't it?

Your switch is this:

switch ( input ) {
    case 1:            /* Note the colon, not a semicolon */
        playgame();
        break;
    case 2:
        loadgame();
        break;
    case 3:
        playmultiplayer();
        break;
    case 4:
        printf( "Thanks for playing!\n" );
        break;
    default:
        printf( "Bad input, quitting!\n" );
        break;
}

Since there's no cases after default, there's no effect whether you write break on default or not. However, you can easily suppose to write a new case.

    default:
        printf( "Thanks for playing!\n" );
        /* what happens if there's no `break`...? */
    case 5:
        the_new_feature();
        break;
}

It's common-mistakes in C/C++. If you add new feature after 5 years and you completely forget it, it'll become a very buggy bug. Some modern language (e.g. C#, ...) even forbid switch-case without break or return.

Conclusion: There's no problem in syntax, but it's very bad practice and using break is highly recommended.

2
  • Is it even allowed to have a case after default?
    – Rodrigo
    Commented May 12, 2020 at 5:27
  • 3
    @Rodrigo Yes it is
    – JakeD
    Commented Jul 10, 2020 at 22:35
10

It depends on How default case is written.

In the following case break is neccessary.

switch ( input ) {
    default:
        printf( "Bad input, quitting!\n" );
        break;
    case 1:            /* Note the colon, not a semicolon */
        playgame();
        break;
    case 2:
        loadgame();
        break;
}
1
  • Correct, but the question meant that the default case was last. I will edit, sorry.
    – gsamaras
    Commented Oct 1, 2014 at 10:17
3

if default case is at last then break statement have no use. if it is before any other case then break is required. since usually we are putting default at the end so you can omit it..

2
  • Not only the default case,but all the cases is applicable here
    – Spikatrix
    Commented Oct 1, 2014 at 10:31
  • @Cool Guy yes.. but he were asking abt default case.
    – Rustam
    Commented Oct 1, 2014 at 10:35
2

There is no difference if you omit or if you leave the break statement. It is a matter of taste.

However, it is good practice to write the break statement for different reasons.

Take a look at programmers exchange.

0
1

Break Seems to Be Optional

break seems to be optional in that case. and also your code will work almost fine.

However

in many situations adding break improves your code and protect your code from errors/exceptions.

it increases readability, consistency and more error free.

some language like C# break(or other control-flow statement that exits the case) is technically needed after the last alternative. dan04

so for better programming practices i suggest one should use it.

see this excellent post for more details.

0

Actually, you don't need the break in default case. And as your checking, it's the same if you don't have break in default case.

But in my opinion, you should have the break in default case, because:

  1. It make your code is have a form in every case.
  2. It's good with current compiler but maybe not with specific other compiler. So you should make it good well to prevent issue from future.
1
  • The behavior here is same for all compilers
    – arye
    Commented Aug 7, 2023 at 5:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.