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
" );
printf( "2. Load game
" );
printf( "3. Play multiplayer
" );
printf( "4. Exit
" );
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!
" );
break;
default:
printf( "Bad input, quitting!
" );
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…