Computer Applications
Which of the following is true about the default label in a switch statement?
Answer
It executes if no matching case label is found.
Reason — The default label in a switch statement is used as a fallback option. If none of the case labels match the switch expression, the default block executes.
- Including a
defaultlabel is optional in aswitchstatement. - It can appear anywhere within the
switchblock. - The
defaultlabel can appear only once, but its position within theswitchblock is flexible (not restricted to the top).
Related Questions
Using the switch-case statement, write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
Letters Unicode A 65 B 66 . . . . . . Z 90 (b) Display the following pattern using iteration (looping) statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5A student executes the following program segment and gets an error. Identify the statement which has an error, correct the same to get the output as WIN.
boolean x = true; switch(x) { case 1: System.out.println("WIN"); break; case 2: System.out.println("LOOSE"); }