SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
Switch Statements
Comparing Exact Values
The Switch Statement
• The switch statement provides
another way to decide which
statement to execute next
• The switch statement
evaluates an expression, then
attempts to match the result
to one of several possible
cases
• The match must be an exact
match.
2
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
The Switch Statement
• Each case contains a
value and a list of
statements
• The flow of control
transfers to
statement
associated with the
first case value that
matches
3
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
Switch - syntax
• The general syntax of a switch statement is:
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
switch
and
case
are
reserved
words
If expression
matches value3,
control jumps
to here
The Switch Statement
• The break statement can be
used as the last statement in
each case's statement list
• A break statement causes
control to transfer to the
end of the switch
statement
• If a break statement is not
used, the flow of control will
continue into the next case
switch ( expression ){
case value1 :
statement-list1
break;
case value2 :
statement-list2
break;
case value3 :
statement-list3
break;
case ...
}
Switch Example
• Examples of the switch statement:
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
Switch – no breaks!!!
• Another Example:
switch (option){
case 'A':
aCount++;
case 'B':
bCount++;
case 'C':
cCount++;
}
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
Switch - default
• A switch statement can have an optional default
case
• The default case has no associated value and simply
uses the reserved word default
• If the default case is present, control will transfer to
it if no other case value matches
• If there is no default case, and no other value
matches, control falls through to the statement after
the switch
The switch Statement
• Switch
with
default
case:
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
To Switch or not to Switch
• The expression of a switch statement must result in an
integral type, meaning an integer (byte, short, int, long)
or a char
• It cannot be a boolean value or a floating point value
(float or double)
• The implicit boolean condition in a switch statement is
equality
• You cannot perform relational checks with a switch
statement

More Related Content

PPTX
Interface in java
PPTX
IT infrastructure and network technologies by Mark John Lado
PPTX
Cloud Computing and Data Centers
PPTX
The Role of ICT in Key Sectors of Society’s Development
PPT
Looping statements in Java
PDF
People management
PDF
Data privacy act of 2012 presentation
PPTX
Advantages and disadvantages of social media
Interface in java
IT infrastructure and network technologies by Mark John Lado
Cloud Computing and Data Centers
The Role of ICT in Key Sectors of Society’s Development
Looping statements in Java
People management
Data privacy act of 2012 presentation
Advantages and disadvantages of social media

What's hot (20)

PPT
Java interfaces
PPTX
Operators in java presentation
PPTX
Operators in java
PPTX
Java input
PPTX
Methods in java
PPTX
Classes objects in java
PPTX
Polymorphism in java
PPT
Method overriding
PPTX
Method overloading
PPTX
Delegates and events in C#
PPTX
virtual function
PPTX
This pointer
ODP
OOP java
PPTX
Exception handling
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Inheritance in java
PPTX
Operators and expressions in C++
PDF
Method Overloading In Java
PPTX
Abstract class and Interface
PPT
Abstract class in java
Java interfaces
Operators in java presentation
Operators in java
Java input
Methods in java
Classes objects in java
Polymorphism in java
Method overriding
Method overloading
Delegates and events in C#
virtual function
This pointer
OOP java
Exception handling
[OOP - Lec 19] Static Member Functions
Inheritance in java
Operators and expressions in C++
Method Overloading In Java
Abstract class and Interface
Abstract class in java
Ad

Viewers also liked (20)

PPTX
The Switch Statement in java
PPTX
Switch statement
PPTX
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
PPTX
Kunnen vogels ruiken
PPT
Java ppt
PPTX
Daffodil International University. Object oriented programming JAVA if else s...
PPTX
1. dml select statement reterive data
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPT
Les01 (retrieving data using the sql select statement)
ODP
Prabu's sql quries
PPTX
Looping and switch cases
PPTX
Switch case and looping
PPSX
Php if else
PPT
SQL select statement and functions
PPTX
SQL Data Manipulation
PPTX
SQL JOIN
PPTX
SQL Basics
PPT
SQL Tutorial - Basic Commands
The Switch Statement in java
Switch statement
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Kunnen vogels ruiken
Java ppt
Daffodil International University. Object oriented programming JAVA if else s...
1. dml select statement reterive data
Database Systems - SQL - DDL Statements (Chapter 3/3)
Les01 (retrieving data using the sql select statement)
Prabu's sql quries
Looping and switch cases
Switch case and looping
Php if else
SQL select statement and functions
SQL Data Manipulation
SQL JOIN
SQL Basics
SQL Tutorial - Basic Commands
Ad

Similar to Switch statements in Java (20)

PPTX
Programming Fundamentals lec 08 .pptx
PPTX
week 3 Programming lecture 06 (1) .pptx
PDF
Control statements anil
PPT
Control structure
PPTX
Switch Case in C Programming
PPT
SWITCH CASE STATEMENT IN C
PDF
Programming in C Conditional statements.pdf
PPTX
Control Structures.pptx
PDF
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
PDF
Lecture9(Selection -Part 2) computers.pdf
PPT
Flow of Control
PPTX
IF & SWITCH (conditional control) in computer science
PDF
itft-Decision making and branching in java
PPTX
Conditional statement in c
PPTX
Selection statements
PPTX
Flow of control C ++ By TANUJ
PPTX
Selection Statements in C Programming
PPTX
Conditional Statement-pptx-lesson3asdfsa
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Programming Fundamentals lec 08 .pptx
week 3 Programming lecture 06 (1) .pptx
Control statements anil
Control structure
Switch Case in C Programming
SWITCH CASE STATEMENT IN C
Programming in C Conditional statements.pdf
Control Structures.pptx
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
Lecture9(Selection -Part 2) computers.pdf
Flow of Control
IF & SWITCH (conditional control) in computer science
itft-Decision making and branching in java
Conditional statement in c
Selection statements
Flow of control C ++ By TANUJ
Selection Statements in C Programming
Conditional Statement-pptx-lesson3asdfsa
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf

More from Jin Castor (16)

PPTX
Herbal Remedies for Migraine and Headache.pptx
PPTX
Information security
PPTX
Introduction to E-commerce
PPTX
Introduction to Infographics Designing
PPTX
Creative designing using Adobe Products
PPTX
Introduction to Adobe Illustrator
PDF
SEO Advanced and scalable link building
PDF
Introduction to Web Designing
PPTX
Introduction to search engine optimization
PPTX
Web services protocols
PPTX
Web application security
PPTX
Introduction to xampp
PPTX
Drupal introduction
PPT
Web security
PPTX
Control statements in Java
PPTX
Java arrays
Herbal Remedies for Migraine and Headache.pptx
Information security
Introduction to E-commerce
Introduction to Infographics Designing
Creative designing using Adobe Products
Introduction to Adobe Illustrator
SEO Advanced and scalable link building
Introduction to Web Designing
Introduction to search engine optimization
Web services protocols
Web application security
Introduction to xampp
Drupal introduction
Web security
Control statements in Java
Java arrays

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPT
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
Spectral efficient network and resource selection model in 5G networks
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced Soft Computing BINUS July 2025.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Teaching material agriculture food technology

Switch statements in Java

  • 2. The Switch Statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • The match must be an exact match. 2 switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... }
  • 3. The Switch Statement • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches 3 switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... }
  • 4. Switch - syntax • The general syntax of a switch statement is: switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } switch and case are reserved words If expression matches value3, control jumps to here
  • 5. The Switch Statement • The break statement can be used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case switch ( expression ){ case value1 : statement-list1 break; case value2 : statement-list2 break; case value3 : statement-list3 break; case ... }
  • 6. Switch Example • Examples of the switch statement: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; }
  • 7. Switch – no breaks!!! • Another Example: switch (option){ case 'A': aCount++; case 'B': bCount++; case 'C': cCount++; } switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; }
  • 8. Switch - default • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • If there is no default case, and no other value matches, control falls through to the statement after the switch
  • 9. The switch Statement • Switch with default case: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; }
  • 10. To Switch or not to Switch • The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char • It cannot be a boolean value or a floating point value (float or double) • The implicit boolean condition in a switch statement is equality • You cannot perform relational checks with a switch statement