0
So I'm guessing that the default statement doesn't require a break because it's already at the end of the switch statement?
4 Antworten
+ 1
break is use to stop compiler and leave the statements after that and leave the scope and default is always at ending so there is no statement after default so no break is required
0
Shortly - yes. Any case that is the last one doesn't require break but it's a bad practice. Also it is not true that default has to be last.
0
default case is run when no other case is true so it is a extra option which is neccesary
0
break is required if you only want one task with one case,eg.
switch(ch){
case 1:
case 2:{ //some task
break;}
case 3://task 3
case 4: return;
default:cout<<"wassup";
}
cout<<"\n nothing";
if ch is 3,task 3 will be executed and then the program will return,
hence output would be whatever task 3 was.
had there been a break after task 3,
output would be
//task 3's output
nothing
hope you understood it :)