Can someone let me know how the default case in a switch statement (C++) works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone let me know how the default case in a switch statement (C++) works?

Is it necessary to put it in the end after all the cases or can we put it somewhere in the mid or in the start of the switch statement like before all the cases and if it's in the start or mid then how is it compiled? As far as I know that compiler compiles the code from top to bottom and if there is a default case before all the other cases then how the switch statement will be executed?

1st Feb 2019, 5:09 PM
Saifullah Rauf
Saifullah Rauf - avatar
5 Answers
+ 3
It's perfectly fine to put the default label on top or in between other labels, let's try this snippet and you can comment/uncomment the default label on top or in the middle, it works just fine. If there is anything to worry about, it would probably be to forget to put a `break;` in the labels, except for the last one, but I personally am used with default as last label, but for compiler, I guess it doesn't matter. #include <iostream> #include <cstdlib> #include <ctime> int main() { int x; srand(time(nullptr)); for(int i {0}; i < 100; i++) { x = rand() % 8 + 1; switch(x) { /* it's fine here default: std::cout << "IT'S " << x << "\n"; break; */ case 1: case 2: case 3: case 4: std::cout << x << "\n"; break; default: // it's also fine here std::cout << "IT'S " << x << "\n"; break; case 5: case 6: case 7: std::cout << x << "\n"; break; } } return 0; }
1st Feb 2019, 6:34 PM
Ipang
+ 3
You're most welcome bro! happy coding 👍
1st Feb 2019, 6:53 PM
Ipang
+ 2
Hey.. Well, a switch case mainly made to match the cases from the passed parameter. Now suppose you have a switch case saying " switch(day) " which takes a number from 1 to 7 as a day and match the case in the switch case body okay? now suppose a user entered something else which is not between 1 to 7 then what would happen? we have only cases for number between 1 to 7 other than that program will get bugged(error) right and that is where default comes in the picture. if your switch case is having default then if any case doesn't matches, it'll handled by default case. ex. switch(day) { case 1: | | | case 7: {} default: cout<<"please enter number between 1 to 7"; break; and yesss you can put default anywhere in the switch case body compiler won't mind it I hope it's clear now...
1st Feb 2019, 5:38 PM
Jayesh Suthar
Jayesh Suthar - avatar
+ 1
What you've explained I know that, I've been using switch in a few programs of mine already, my question is let's suppose we type switch(day){ default: cout << "please enter number between 1 to 7"; break; case 1: | | | case 7: } I wanna know in this case what will happen because I just encountered a code like this in one of my coding challenges here on solo learn and I don't know whether we can place default in some other place than the end of the cases and what will happen if we do so
1st Feb 2019, 5:46 PM
Saifullah Rauf
Saifullah Rauf - avatar
+ 1
Thanks bro, this answers my question. So basically compiler looks for a default case after looking all the other cases regardless of the position of default case
1st Feb 2019, 6:41 PM
Saifullah Rauf
Saifullah Rauf - avatar