Adamson University College of Engineering Computer Engineering Department
Adamson University College of Engineering Computer Engineering Department
College of Engineering
Computer Engineering Department
Experiment No. 4
CONDITIONAL switch STATEMENT
SCORE
Name:
Schedule:
Date:
Instructor:
Experiment No. 4
CONDITIONAL switch STATEMENT
I. OBJECTIVES
a. To implement the different conditional switch statement in C++ programs.
b. To be able to identify the application and limitations of the switch statement
to if statements.
II. DISCUSSION
To implement multi-way decisions the switch statement provides a more concise
representation than multiple else if statements, which can get very difficult to follow.
The switch statement is a multi-way decision that tests whether an expression
matches one of a number of constant integer values. If it matches, then the statements
which follow are all executed.
switch (expression)
{
case const_expr_1:
statements 1;
break;
case const_expr_2:
statements 2;
break;
:
:
default:
statements n;
}
Finally, if the value of expression has not matched any of the previously specified
constants, the program will execute the instructions included in the default: section, if this
one exists, since it is optional.
III. Procedures:
Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.
1.
2.
1. Write a program that asks a user to enter two numbers. The user will then choose an
operation by entering the corresponding letter:
a – addition
s – subtraction
m – multiplication
d–division
The program must evaluate the two numbers based on the chosen operation and then
display the output.
Sample Output 1:
Please enter the first number: 8
Please enter the second number: 10
Please enter your desired operation: m
The product of the two numbers you entered is 80.
Sample Output 2:
Please enter the first number: 5
Please enter the second number: 9
Please enter your desired operation: a
The sum of the two numbers you entered is 14.
2. You are tasked by your teacher to help her in the computation of your grade for
preliminary period. The grade will be computed based on the following formula:
Semestral Grade =(Prelim Grade * 30%) + (Midterm Grade * 30%) + (Final Grade *
40%)
The program will return a letter grade based on the given conditions:
Letter Grade Score range
A 100
B 90-99
C 80-89
D 75-79
F Below 75
Sample Output:
Please enter your Prelim Grade: 80
Please enter your Midterm Grade: 70
Please enter your Final Grade: 75
Your Semestral Grade is 75.
Equivalent letter is D.