0% found this document useful (0 votes)
169 views4 pages

C++ Switch..case Statement

The document discusses the C++ switch-case statement for executing different blocks of code based on the value of an expression, providing the syntax and an example of a calculator program that uses switch-case to perform basic math operations like addition, subtraction, multiplication, and division based on the user's input.

Uploaded by

Kenneth Bautista
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)
169 views4 pages

C++ Switch..case Statement

The document discusses the C++ switch-case statement for executing different blocks of code based on the value of an expression, providing the syntax and an example of a calculator program that uses switch-case to perform basic math operations like addition, subtraction, multiplication, and division based on the user's input.

Uploaded by

Kenneth Bautista
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/ 4

C++ switch..

case Statement
In this tutorial, we will learn about switch statement and its working in C++
programming with the help of some examples.

The switch statement allows us to execute a block of code among many


alternatives.
The syntax of the switch statement in C++ is:

switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;

case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}

How does the switch statement work?


The expression is evaluated once and compared with the values of
each case label.
• If there is a match, the corresponding code after the matching label is
executed. For example, if the value of the variable is equal to constant2 ,

the code after case constant2: is executed until the break statement is
encountered.
• If there is no match, the code after default: is executed.
Note: We can do the same thing with the if...else..if ladder. However, the
syntax of the switch statement is cleaner and much easier to read and write.

Flowchart of switch Statement

Flowchart of C++ switch...case statement


Example: Create a Calculator using the switch Statement
TASK 1 FOR FINALS: Type the given code below, and find out what will be the output.

In the above program, we are using the switch...case statement to perform


addition, subtraction, multiplication, and division.
How This Program Works
1. We first prompt the user to enter the desired operator. This input is then
stored in the char variable named oper .

2. We then prompt the user to enter two numbers, which are stored in the
float variables num1 and num2 .
3. The switch statement is then used to check the operator entered by the
user:
o If the user enters + , addition is performed on the numbers.
o If the user enters - , subtraction is performed on the numbers.
o If the user enters * , multiplication is performed on the numbers.
o If the user enters / , division is performed on the numbers.
o If the user enters any other character, the default code is printed.

Notice that the break statement is used inside each case block. This terminates
the switch statement.
If the break statement is not used, all cases after the correct case are
executed.

Task 2: Create a Program using Switch statement by following this example:

Enter operator keyword: S


Enter two operands: 3.4 8.4
3.4 - 8.4 = -5.0

CONDITIONS:

A, a, + will be the keyword for Addition

S, s, - will be the key word for Subtraction

M, m, X will be the keyword for Multiplication

D, d, / will be the keyword for Division

Screenshot the code and input/output. Take a photo while coding. Submit thru PDF! Good luck! <3

You might also like