3️⃣

5.3 The Default Keyword

 
 
You might have noticed the default case at the end of the switch statement we used in the previous examples. Let’s talk about this case a little more in depth. The default case is only executed if the computer decides that the value of the variable in question does not match any of the other cases. Analogically, the default case in a switch statement is like the else statement in an if/else conditional structure - it is only executed if none of the previous possibilities work out. When you use a switch statement, you will not always need a default case. The code will compile even if you do not include a default case. However, whenever it is not possible to account for all possible cases without having extreme amounts of code, the default case is very useful.

Previous Section

Next Chapter

 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.