Switch statement is a multiway branching which provides an alternative way too lengthy if-else comparisons. It selects a single block to be executed from a listing of multiple blocks on the basis of the value of an expression or state of a single variable. A switch statement using multiple value cases correspond to using more than one value in a single case. This is achieved by separating the multiple values in the case with a comma.
Input : january Output : Winter. Input : september Output : Autumn.
Rather than making different individual cases for months having the same season, we clubbed different months with the same output. This saves us to write redundant pieces of code.
Input : 6 Output : You entered an even number. Input : 5 Output : You entered an odd number.
Instead of writing 10 different cases to check whether the entered number is even or not, we could simply do the same in 2 switch cases using multiple case values.
Input : g Output : Lowercase alphabet character. Input : F Output : Uppercase alphabet character.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.