0% found this document useful (0 votes)
13 views49 pages

Switch Case - Java

Switch case in Java with questions

Uploaded by

asthaverma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views49 pages

Switch Case - Java

Switch case in Java with questions

Uploaded by

asthaverma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

A multi-branch selection statement in 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.

 It is generally used when a value has to be


tested against a list of values for equality.

 It cannot be used in situations where a range


of values or more complex conditions need to
be checked.
a 5
to

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

 The variable is tested for equality against


each case and when a match is found, the
statements associated with that constant are
executed until the break statement or end of
the switch statement is reached.
Case January
switch(variable / expression)  Variable must be byte, short,
{ int, char or String
case value1 :
-----;  If it is an expression, it must be
-----; an arithmetic expression that
a results in a value which must be
break;
switch one of the above types.

case value2 : value1, value2…value n must be
case 5

------; constants/literals of the above
------; data types only.
I
break;
Break must be given at the end
break 
of each case.
case value 3 :
------statement;  Default statement is optional
------; 10
break;
case  Note: switch …. Case does not
support comparisons like
greater than(>), less than(<),
default : greater than equal to (>=), less
------;
------;
break than equal to (<=) or not equal
to (!=).
} // end of switch 5
3 if a
10
I
had
poofter
veg Thug
y
Stig ch ng
ch
switch
case Veg
if
a
Switch

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?

Do you think default needs


to be written at the end
only?
 It is like the “else” in an “if” construct.

 It gets executed when no match is found.

 It is optional. If the default statement is not


part of the switch case, then no action takes
place, if all matches fail.

 It need not be written at the end of all the


case statements- it can be written anywhere
within the switch.
Switch a

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

the sit take


if
deke Ee
it
if Ineffabe if
if Else
Literal
int a g T

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.

 It is a faster and a compact alternative to the


multiple “if…else if’ constructs.
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.
 identify the need of a fall through in a switch case
construct
 evaluate a program that uses fall through and trace
the output.
 apply nested switch in a program
➢ To evaluate a program that has a switch
construct and trace the output.
➢ To identify the need for a fall through
➢ To apply fall through in a program
➢ To understand and apply nested switch
thy
mum
int num=sc.nextInt();
tne
if(!(num>=11 && num<=13)) {
What will be the
Q switch(num%10) {
case 1:
output if num is 23?

System.out.println(num+”st”); What will be the


break;
case 2: 0
output, if num is 13?

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.

 Based on the month number that is input, the


output should be “First Quarter” or “Second

I
Quarter” or “Third Quarter” or “Fourth Quarter”.

 The program displays error message, if the month


number is incorrect.
chart
e se next
char

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.

 You may assume that the user enters an


uppercase letter.

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

You might also like