Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we break the default case in switch statement?

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?

like image 317
gsamaras Avatar asked Oct 01 '14 10:10

gsamaras


People also ask

Do I need break in default switch case?

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Why do we need break default statement in switch case?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

Should default be last in switch case?

A 'switch' statement should have 'default' as the last label. Adding a 'default' label at the end of every 'switch' statement makes the code clearer and guarantees that any possible case where none of the labels matches the value of the control variable will be handled.

Is default case optional inside switch?

The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.


2 Answers

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.

like image 64
David Ranieri Avatar answered Sep 20 '22 09:09

David Ranieri


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.

like image 29
ikh Avatar answered Sep 19 '22 09:09

ikh