[go: up one dir, main page]

0% found this document useful (0 votes)
24 views20 pages

2 Switch Case

A switch case statement provides an easy way to dispatch code execution based on the value of a variable, serving as a faster and more compact alternative to multiple if-else if statements. It allows a variable to be tested for equality against a list of case values, with the associated statements for the matching case being executed until a break is reached. Key differences between switch and if include switch only supports equality checks while if can also check ranges, and switch allows falling through cases without breaks while if requires else blocks.

Uploaded by

Aakriti Bansal
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)
24 views20 pages

2 Switch Case

A switch case statement provides an easy way to dispatch code execution based on the value of a variable, serving as a faster and more compact alternative to multiple if-else if statements. It allows a variable to be tested for equality against a list of case values, with the associated statements for the matching case being executed until a break is reached. Key differences between switch and if include switch only supports equality checks while if can also check ranges, and switch allows falling through cases without breaks while if requires else blocks.

Uploaded by

Aakriti Bansal
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/ 20

A multi-branch selection statement in Java

Lesson 1
 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 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
Give the output of the code snippet
int amt=15000;
if(amt <10000)
System.out.println("No discount");
if(amt<25000)
System.out.println("Discount 10%");
if(amt<50000)
System.out.println("Discount 15%");
if(amt<75000)
System.out.println("Discount 20%");
else
System.out.println("Discount 25%");
 Discount 10%
 Discount 15%
 Discount 20%
Spot the errors in the code snippet. After
correcting the errors, what will be the output, if
the value of ch is 78.
int ch=sc.nextInt();
if(ch=32)
System.out.println("Represents space");
else if(ch>=48 && <=57)
System.out.println("Represents digits");
else if(ch>=65 && ch<=90 && ch>=97 && ch<=122)
System.out.println("Represents Letter");
else
System.out.println("Represents special characters");
import java.util.*;
class switch1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int ch=sc.nextInt();
if(ch==32)
System.out.println("Represents space");
else if(ch>=48 && ch<=57)
System.out.println("Represents digits");
else if(ch>=65 && ch<=90 && ch>=97 && ch<=122)
System.out.println("Represents Letter");
else
System.out.println("Represents special characters");
}
}
 Represents special characters
 A “switch… case” is a more compact
statement and a faster alternative to multiple
“if….. else if” constructs.

 It is generally used when a value must 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 multiple-branch selection statement that allows a
variable or the result of an arithmetic expression to
be tested for equality against a list of values.

 Each value is called a case, which can be a


literal/constant of the following data types only:
byte, short, int, char or String

 The variable or the result of the arithmetic


expression 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 is reached or the end of the switch
block is reached.
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
results in a value which must be
break; one of the above types.

case value2 :  value1, value2…value n must be
------; constants/literals of the above
------; data types only.
break;
 break is usually given at the end
of each case, but it is optional.
case value 3 :
------statement;  default statement is optional
------;
break;  Note: switch …. case does not
support comparisons like
greater than(>), less than(<),
default : greater than equal to (>=), less
------; than equal to (<=) or not equal
------; to (!=).
} // end of switch
Knowledge
check
public static void main()
{ What punctuation do you use for
int age=3; your cases?
• Semi colon ;
switch(age)
• colon :
{
case 1
System.out.println(“you can crawl”);
break; What does break do?

case 2:
System.out.println(“you can talk”);
break;
case 3: What do you think default does?
System.out.println(“you can run”);
break; Do we need to add a break
default: statement after default?
System.out.println(“you can talk”);
}
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 an optional statement. 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.
 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, the


program control flows to the next case below the
matching case and it will continue to execute,
regardless of the expression of the subsequent case
labels, till a break is encountered or till the end of
the end of the switch block is encountered. It results
in a fall-through.

 Note: There should be no statement after the break


statement (except the next case statement), because
the compiler gives the error “Unreachable code”.
Difference between switch and if
Let’s revisit the LI and SC

SUCCESS CRITERIA
LEARNING INTENTION
Students should be able to:
identify the need for a multi-branch selection
Switch…case, a multi-branch


construct
selection construct, provides an
understand the syntax of a switch case construct
easy way to dispatch execution to 

different parts of code based on  apply the switch construct in a program


the value of a variable or the  compare switch construct and if construct
result of an arithmetic  evaluate a program that has a switch construct and
expression. trace the output.
 create user-friendly programs using switch..case
 It is a faster and a compact
alternative to the multiple
“if…else if’ constructs.

You might also like