C++ TutorialLesson 15Free preview
C++ Switch
This lesson is part of C++ · All levels
Summary
This lesson explains the switch statement as a branching alternative when comparing one variable with many constant values. Every possibility is written in a case, and default is the last option when there is no case matching. The use of a break prevents the execution from falling to the next case.
Key points
- The switch is suitable for comparing the values of integers or char accurately.
- Each case is terminated break so that there is no fall-through.
- The default is optional and executed when there is no suitable case.
- The switch can't directly check the range of values like >= and <.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.