Switch Case - Java
Switch Case - Java
s o
int
5
A b g
H
a
St
I
Switch …case, a multi-branch selection
construct, provides an easy way to dispatch
execution to different parts of code based on
the value of the expression, which could be
an integral or String type only
Students should be able to:
identify the need for a multi-branch selection
construct
understand the syntax of a switch case
construct
apply the switch construct in a program
compare switch construct and if construct
evaluate a program that has a switch
construct and trace the output.
create user-friendly programs using
switch..case
To identify the need for a multi-branch
selection construct
To understand the syntax of a switch case
construct
To apply the switch construct in a program
To compare switch construct and if construct
1. Which command in a Java switch statement is the
catch-all for values that don't meet the criteria?
•return
•default return
I
t
•exit
•break default
exit
a break
2. The _____ statement tells the computer to skip
to the end of the switch statement
•default
•return
default
Md
•break
•exit atom
break f
exit
A “switch… case” is a more compact
statement and a faster alternative to the “if”
construct.
12
he also
if a 5
A 20
I
E T
d
t Sc nextfloats
float
switch Ct
A multiple-branch selection statement that
allows a variable or the result of an
expression to be tested for equality against a
list of values.
p float doubbiolet
Each value is called a case, which can be a
literal/constant of the following data types
E
only: byte, short, int, char or String
default
a not fond
Sop
IbrakI
5
A Case
Sop s
triad
is
class apples
{
public static void main()
{
char
int age=3;
What punctuation do you
switch(age) use for your cases?
{ Semi colon ;
case 1 I colon :
System.out.println(“you can crawl”);
break; What does break do?
case 2:
System.out.println(“you can talk”);
break;
case 3:
System.out.println(“you can run”);
break; What do you think default
default: does?
System.out.println(“you can talk”);
} Do we need to add a break
} statement after default?
cases
b
if 10
Sop a ist
b is 10
i
break systemic
4
if a
break
A “break” inside a switch statement transfers
the control to the line of code that is outside
the body of the Switch statement.
If “break” is not given at the end of each case,
then the program control flows to the next case
below the matching case and it will continue to
execute till a break is encountered or the
switch statement’s end is encountered. It
results in a fall-through.
Note: There should be no statement after the
break statement (except the case statement),
because the compiler gives the error
“Unreachable code”.
case Ii
Sop i
out
Gop breaking
immunity
murmur
case 2
Difference between switch and if
a 2 a cs
E
ay
char c
g
the.tt
a v good 4
80 god
c
460 good
good
int b Ig
variable
Stig p Hello i Literal
t i variable
Stig L
Lesson 2
Switch…case, a multi-branch selection
construct, provides an easy way to dispatch
execution to different parts of code based on
the value of a variable or the result of an
arithmetic expression.
System.out.println(num+”nd”);
If break;
case 3: I
System.out.println(num+”rd”);
break;
23rd
default:
System.out.println(num+”th”);
break; T Le T T
}//switch
}//if
else
t
T
System.out.println(num+”th”);
BI
A Fall through occurs when there is no
break statement in a switch case block
and all the statements after the matching
case label are executed in sequence,
(even if the value of the
É
switch variable
or test expression does not match with
the subsequent case labels), until the
next break statement is encountered, or
end of the switch block is reached.
Switch g a
D
case A be
a
Sop
Case b
Sop Ibd
case C
Sop c
Fit
a
Switch
case 5
case i o
Sop L'Xyz
break Y
class Example{ case 4:
Pgt
D
System.out.println("Automation
public static void main( ) testing");
{
case 5:
int courses = 2; System.out.println("Hadoop");
switch(courses)
{ case 6:
if course
System.out.println("AWS");
case 1: default:
System.out.println("java"); System.out.println("check out
Xcase 2:
if fuses
edureka.co for more");
L }
System.out.println("python");
}
python
of
case 3: }
System.out.println("Devops");
Guess the output! gap
DevopAbomtF
python
Devops
j
Automation testing
Hadoop
AWS
check out edureka.co for more
A fall through need not always be a logical error as
shown in the previous slides. It can be used
effectively to represent an ‘or’ construct in a
switch..case as shown in the next example.
I
Quarter” or “Third Quarter” or “Fourth Quarter”.
I 3 a first Qtr
4 6
II at
7 9 II Qt
Qt
10 12
I
e
int month = 5;
switch(month)
{
I I fall
case 1:
case 2: through
case 3:
System.out.println("First Quarter");
break;
case 4:
case 5:
case 6:
System.out.println("Second Quarter");
break;
case 7:
case 8:
case 9:
System.out.println("Third Quarter");
break;
case 10:
case 11:
case 12: i
System.out.println("Fourth Quarter");
break;
default:
System.out.println(“error”);
}
Test your
understanding
Discuss with your elbow partner and list
scenarios where a fall through can be used to
represent the ‘or’ construct in programs.
Sc next Intl
int month
s
not
X if 102 Pg
D
int code = 2; month noa
switch(code)
{
Take an input
case 1:
as
the date
default System.out.println("Wish"); paint
member
case 2:
bad
System.out.println("You");
1 3 First
default:
case 3:
50
System.out.println("A"); 9 6
7 9
2nd
3
System.out.println("Happy");
break; to 12 Y
case 4:
System.out.println("New");
case 5:
System.out.println("Year");
you
A
}
Happy
Switch
You
A
Happy
Write a program that accepts a character from
the user, checks if it is a vowel or a
consonant and displays an appropriate
message.
for 148323 Pg
102
103
y
iimport java.util.Scanner;
class Menu {
public static void main() {
Scanner obj = new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. North Indian"); Nested switch is basically a switch
System.out.println("2. South Indian");
int ch1; construct that is placed inside
char ch2;
System.out.println("Enter your choice- 1 or 2");
another ‘switch’ construct.
ch1 = obj.nextInt();
if(ch1==1) {
Replace the ‘if’ constructs in the
System.out.println("Menu for North Indian"); given program with ‘switch’
System.out.println("a. Shahi Paneer");
System.out.println("b. Rajma Rice"); constructs
System.out.println("Enter your choice");
ch2 = obj.next().charAt(0);
if(ch2=='a')
System.out.println("You chose Shahi Paneer");
else if(ch2=='b')
System.out.println("You chose Rajma Rice");
}
else if(ch1==2) {
System.out.println("Menu for South Indian");
System.out.println("a. Idli");
System.out.println("b. Vada");
ch2 = obj.next().charAt(0);
if(ch2=='a')
System.out.println("You chose Idli ");
else if(ch2=='b')
System.out.println("You chose Vada");
}
else
System.out.println("Incorrect option");
}//main
}//class
Menu
Food
South Ind
Noth Ind
Paneer
selected paneer
you
Case E
anime
chart o
Smith ath
A
case
Casi
if C
d
guitar
default
sop
else
ne N I
5 I
set
5 I NI
E
Nested switch
a switch
inside
if inside an if
switch
the
War
to
find
of
prime factors
a number
10
a bolos
Discuss with your elbow partner and list
scenarios where a Nested switch can be used in
programs.
BENTO
t ID.my
in a 5 2
Indian
É
Italian
E