SlideShare a Scribd company logo
6
Most read
8
Most read
9
Most read
SWITCH CASES
Presented by BARANI G.
1
Switch cases:
◦ The switch statement evaluates an integral expression and chooses one of
several execution paths based on the expression’s value.
◦ In other words, a Switch statement provides a convenient way of selecting
among a (possibly large) number of fixed alternatives.
2
General form:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
3
How switch statements work:
◦ The switch expression is evaluated once.
◦ The value of the expression is compared with the values of each case.
◦ If there is a match, the associated block of code is executed.
The break and default keywords are optional.
4
Flow chart of switch statement:
5
The break keyword:
◦ When C++ reaches a break keyword, it break out of the switch block.
◦ This will stop the execution of more code and case testing inside the block.
◦ When match is found, and the job is done, it’s time for a break. There is o need
for more testing.
A break can save a lot of execution time as it ignores the execution of all the
rest of the code in the switch block.
6
The default keyword:
◦ The default code specifies some code to run if there is no case match.
◦ The default keyword must be used as the last statement in the switch, and it
does not need a break.
7
Example: Day of the week
#include <iostream>
using namespace std;
int main() {
int day = 5;
switch (day) {
case 1:
cout << "Monday ";
break;
case 2:
cout << "Tuesday";
break;
8
case 3: Cont…
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
9
case 7: Cont…
cout << "Sunday";
break;
default :
cout<< "Invalid input" ;
}
return 0;
}
OUTPUT:
Friday
10
Thank you!
11

More Related Content

PPTX
C++ decision making
PDF
10. switch case
PPTX
If else statement in c++
PPTX
Presentation on C Switch Case Statements
PPTX
Switch Case in C Programming
PPTX
Operators and expressions in C++
PPTX
Conditional statement c++
PPSX
Break and continue
C++ decision making
10. switch case
If else statement in c++
Presentation on C Switch Case Statements
Switch Case in C Programming
Operators and expressions in C++
Conditional statement c++
Break and continue

What's hot (20)

PPTX
Switch statement, break statement, go to statement
PPTX
PPTX
Functions in c language
PPTX
Introduction to c++
PPTX
Methods in java
PPTX
Decision making and branching in c programming
PPTX
Functions in c++
PPS
Wrapper class
PPTX
The Loops
PPT
RECURSION IN C
PPTX
Function in c program
PPTX
C++ language basic
PPT
Input and output in C++
PPTX
Data types in C
PPTX
Variables in python
PPT
Switch statements in Java
PPTX
Conditional and control statement
PDF
Control Structures in Python
PPTX
Switch statement, break statement, go to statement
Functions in c language
Introduction to c++
Methods in java
Decision making and branching in c programming
Functions in c++
Wrapper class
The Loops
RECURSION IN C
Function in c program
C++ language basic
Input and output in C++
Data types in C
Variables in python
Switch statements in Java
Conditional and control statement
Control Structures in Python
Ad

Similar to Switch case in C++ (20)

PPTX
Programming Fundamentals lec 08 .pptx
PPTX
week 3 Programming lecture 06 (1) .pptx
PPT
4b C switch structure .ppt
PPTX
Switch Case in C Program
PPTX
Switch case
PPT
Flow of Control
PPTX
Introduction to Selection control structures in C++
PPT
Final requirement
PPTX
Lab # 3
PPT
My programming final proj. (1)
PPTX
Switch Case Statement
PPTX
C++ programming Unit 5 flow of control
DOCX
Getting familair with java
PDF
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
PDF
Lecture13 control statementswitch.ppt
PPTX
Final project powerpoint template (fndprg) (1)
PPTX
Switch case.pptx
PPTX
SWITCH-CASE, Lesson Computer Programming.pptx
PPT
ch4_additional.ppt
PPTX
Switch Statements.pptx
Programming Fundamentals lec 08 .pptx
week 3 Programming lecture 06 (1) .pptx
4b C switch structure .ppt
Switch Case in C Program
Switch case
Flow of Control
Introduction to Selection control structures in C++
Final requirement
Lab # 3
My programming final proj. (1)
Switch Case Statement
C++ programming Unit 5 flow of control
Getting familair with java
controlflowwSoftware Development Fundamentals (SDF) – I ODD 2024 Jaypee Insti...
Lecture13 control statementswitch.ppt
Final project powerpoint template (fndprg) (1)
Switch case.pptx
SWITCH-CASE, Lesson Computer Programming.pptx
ch4_additional.ppt
Switch Statements.pptx
Ad

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPT
Introduction Database Management System for Course Database
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
System and Network Administration Chapter 2
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
How Creative Agencies Leverage Project Management Software.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Odoo POS Development Services by CandidRoot Solutions
Softaken Excel to vCard Converter Software.pdf
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Design an Analysis of Algorithms I-SECS-1021-03
Digital Strategies for Manufacturing Companies
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How to Migrate SBCGlobal Email to Yahoo Easily
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Online Work Permit System for Fast Permit Processing
Introduction Database Management System for Course Database
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms II-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administration Chapter 2
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
How Creative Agencies Leverage Project Management Software.pdf

Switch case in C++

  • 2. Switch cases: ◦ The switch statement evaluates an integral expression and chooses one of several execution paths based on the expression’s value. ◦ In other words, a Switch statement provides a convenient way of selecting among a (possibly large) number of fixed alternatives. 2
  • 3. General form: switch(expression) { case x: // code block break; case y: // code block break; default: // code block } 3
  • 4. How switch statements work: ◦ The switch expression is evaluated once. ◦ The value of the expression is compared with the values of each case. ◦ If there is a match, the associated block of code is executed. The break and default keywords are optional. 4
  • 5. Flow chart of switch statement: 5
  • 6. The break keyword: ◦ When C++ reaches a break keyword, it break out of the switch block. ◦ This will stop the execution of more code and case testing inside the block. ◦ When match is found, and the job is done, it’s time for a break. There is o need for more testing. A break can save a lot of execution time as it ignores the execution of all the rest of the code in the switch block. 6
  • 7. The default keyword: ◦ The default code specifies some code to run if there is no case match. ◦ The default keyword must be used as the last statement in the switch, and it does not need a break. 7
  • 8. Example: Day of the week #include <iostream> using namespace std; int main() { int day = 5; switch (day) { case 1: cout << "Monday "; break; case 2: cout << "Tuesday"; break; 8
  • 9. case 3: Cont… cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; 9
  • 10. case 7: Cont… cout << "Sunday"; break; default : cout<< "Invalid input" ; } return 0; } OUTPUT: Friday 10