0

The default case must appear at the end of the switch. It's wrong

i am new .but i think it's wrong in the feed.

13th Apr 2017, 1:49 PM
pokercc zhang
pokercc zhang - avatar
5 Respostas
+ 7
default case works when all the cases won't work. Hence, it must be placed at last
13th Apr 2017, 2:23 PM
Sachin Artani
Sachin Artani - avatar
+ 2
If you place the default case first then it will only evaluate that. Example's Below. int age = 30; switch(age) { default: cout << "You not 30?" << endl; break; case 30: cout << "You are 30...." << endl; break; } In the above example the first thing to be processed is the default statement and that result in the case 30 statement not to become evaluated. //try now at the end int age = 30; switch(age) { case 30: cout << "You are 30...." << endl; break; default: cout << "You are not 30?" << endl; break; } In the above example the case 30 statement is processed first this means that the switch has a chance to processes the age variable being 30. In the first example it does not have a chance to process the case 30 statement because default means nothing above me in the switch statement was true, therefore process the code under me.
13th Apr 2017, 5:35 PM
Joshua Pierson
Joshua Pierson - avatar
0
No Zhang, You are wrong
13th Apr 2017, 2:33 PM
Dago VF
Dago VF - avatar
0
it is also work when default in the middle of switch . https://code.sololearn.com/cLA4KIn97gdv/?ref=app
14th Apr 2017, 11:41 AM
pokercc zhang
pokercc zhang - avatar
0
manukyanzoro9@gmail.com
12th Jul 2017, 5:47 PM
zm1987
zm1987 - avatar