SlideShare a Scribd company logo
1 of 9
Switch Statements
By Enrico Valdez
Credit: Jin Castor
What is a switch statement?
● The switch statement provides another way to decide which statement to execute next, which is
similar to an if statement. However if statements are two way statements while switch statements
are multiway statements meaning it can check more then true or false.
● The switch statement evaluates an expression, then attempts to match the result to one of several
possible cases.
● Match must be an exact match to execute the code within the within the switch statement.
Syntax of a switch statement
A Switch statement contains an expression, cases, breaks, and a default. The expression is a variable for which the
cases check if they have the same value. Case statements are written with the word case and the value that you want
to compare with the expression written next to it. A break statement jumps to the end of the switch statement so
that the code does not have to run all of the cases, if it is already true for one. Defaults are the else statements of
switch statements they are executed if none of the cases are correct.
to Switch or not to Switch
● the value of a case cannot be a variable, boolean, float, or a double
● The case values can also not repeat
● Break statements and Defaults are not necessary when writing switch statements.
Syntax of a switch statement cont.
Flow of Control
● The flow of control transfers to the statement associated with the first case value that matches. If
a break statement is not used, the flow of control will continue into the next case until it reaches a
break statement.
Tracing #1
This will go through every case and
only print the default as none of the
cases match the expression.
Tracing #2
This will only check the
first two case statements
because the break
statement ends the
switch statement. The
third case and the
default statement will not
be checked.
*case statements do not
have to be in numerical
order
Tracing #3
Even though the first
case was true the
second statement will
still print because
there was not a break
statement in the first
case.

More Related Content

Similar to Switch Statements.pptx

IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
RaianaTabitha
 

Similar to Switch Statements.pptx (20)

Final requirement
Final requirementFinal requirement
Final requirement
 
Difference between switch and ladder if
Difference between switch and ladder ifDifference between switch and ladder if
Difference between switch and ladder if
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
SWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptxSWITCH-CASE, Lesson Computer Programming.pptx
SWITCH-CASE, Lesson Computer Programming.pptx
 
Conditional Statement - Switch Case.pptx
Conditional Statement - Switch Case.pptxConditional Statement - Switch Case.pptx
Conditional Statement - Switch Case.pptx
 
10. switch case
10. switch case10. switch case
10. switch case
 
Conditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsaConditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsa
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
IF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer scienceIF & SWITCH (conditional control) in computer science
IF & SWITCH (conditional control) in computer science
 
conditional.pptx
conditional.pptxconditional.pptx
conditional.pptx
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java Statements
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 

Recently uploaded

Recently uploaded (20)

Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
 
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 

Switch Statements.pptx

  • 1. Switch Statements By Enrico Valdez Credit: Jin Castor
  • 2. What is a switch statement? ● The switch statement provides another way to decide which statement to execute next, which is similar to an if statement. However if statements are two way statements while switch statements are multiway statements meaning it can check more then true or false. ● The switch statement evaluates an expression, then attempts to match the result to one of several possible cases. ● Match must be an exact match to execute the code within the within the switch statement.
  • 3. Syntax of a switch statement A Switch statement contains an expression, cases, breaks, and a default. The expression is a variable for which the cases check if they have the same value. Case statements are written with the word case and the value that you want to compare with the expression written next to it. A break statement jumps to the end of the switch statement so that the code does not have to run all of the cases, if it is already true for one. Defaults are the else statements of switch statements they are executed if none of the cases are correct.
  • 4. to Switch or not to Switch ● the value of a case cannot be a variable, boolean, float, or a double ● The case values can also not repeat ● Break statements and Defaults are not necessary when writing switch statements.
  • 5. Syntax of a switch statement cont.
  • 6. Flow of Control ● The flow of control transfers to the statement associated with the first case value that matches. If a break statement is not used, the flow of control will continue into the next case until it reaches a break statement.
  • 7. Tracing #1 This will go through every case and only print the default as none of the cases match the expression.
  • 8. Tracing #2 This will only check the first two case statements because the break statement ends the switch statement. The third case and the default statement will not be checked. *case statements do not have to be in numerical order
  • 9. Tracing #3 Even though the first case was true the second statement will still print because there was not a break statement in the first case.