Switch statement need to have a "default" case? Or you can ignore it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Switch statement need to have a "default" case? Or you can ignore it?

Just as the title says, do you must declare a default case in a switch statement? What happens if you don't set it and the program fall in a case not decleared?

4th Feb 2017, 5:16 PM
Juani Marcos
Juani Marcos - avatar
3 Answers
+ 5
default case only works when all of the other switch cases doesnt work. If you do not put in a default case and all of the other switch cases dont work, then nothing will happen.
4th Feb 2017, 5:40 PM
Wen Qin
Wen Qin - avatar
+ 2
if default statement is not declared and switch cannot match with any case it will exit ...
4th Feb 2017, 6:33 PM
Anandkumar C B
+ 1
default is like an else statement, you dont need an else statement in an if, same goes with default, you dont need it with a switch
4th Feb 2017, 5:48 PM
Dawzy
Dawzy - avatar