0% found this document useful (0 votes)
73 views9 pages

Adamson University College of Engineering Computer Engineering Department

The document discusses a laboratory experiment on conditional switch statements in C++. It provides objectives, discussion on when to use switch statements over if-else statements, syntax examples, and procedures for students to encode programs using switch statements. Students are asked to write programs that use switch statements to evaluate operations on two numbers and compute a student's semester grade based on prelim, midterm, and final exam scores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views9 pages

Adamson University College of Engineering Computer Engineering Department

The document discusses a laboratory experiment on conditional switch statements in C++. It provides objectives, discussion on when to use switch statements over if-else statements, syntax examples, and procedures for students to encode programs using switch statements. Students are asked to write programs that use switch statements to evaluate operations on two numbers and compute a student's semester grade based on prelim, midterm, and final exam scores.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ADAMSON UNIVERSITY

College of Engineering
Computer Engineering Department

Computer Fundamentals and Programming

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.

Several points need watching:


 you must not use floats in switch statements.
 expression evaluated by switch must evaluate to an integer.
 the expression evaluated by switch should match a case. This means that the
matching case must also be an integer or a constant expression which evaluates to
an integer. By `constant expression' I mean that variables are not allowed.
 a default case should be included to catch the integers which don't match any of the
cases. You will see examples of these shortly.
 letters (not words) are seen as integers by C and C++. So letters (not words) can be
used in case statements.
 once the case statement has been matched then all the following statements in the
switch statement are executed unless you specifically stop them. So if the first case
statement happens to be matched, then every single one of the statements in the
entire switch statement will be run unless you put a stop to it. This behavior is very
different from the if ... else if statements.
 you stop execution of any further statements by using a break statement. This
breaks out of the switch statement and continues execution with whatever code
follows the closing brace of the switch.

switch (expression)
{
case const_expr_1:
statements 1;
break;
case const_expr_2:
statements 2;
break;
:
:
default:
statements n;
}

Computer Fundamentals and Programming I Page 2


The default option allows for statements that should be carried out when none of
the specific case situations match. This may not be too clear at the moment, but the
following examples should help.

It works in the following way: switch evaluates expression and checks if it is


equivalent to constant1, if it is, it executes block of instructions 1 until it finds the break
keyword, then the program will jump to the end of the switch selective structure.

If expression was not equal to constant1 it will check if expression is equivalent to


constant2. If it is, it will execute block of instructions 2 until it finds the break keyword.

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.

Both of the following code fragments are equivalent:


switch example if-else equivalent
switch (x) { if (x == 1) {
case 1: cout<< "x is 1";
cout<< "x is 1"; }
break; else if (x == 2) {
case 2: cout<< "x is 2";
cout<< "x is 2"; }
break; else {
default: cout<< "value of x unknown";
cout<< "value of x unknown"; }
}
Notice the inclusion of the break instructions at the end of each block. This is
necessary because if, for example, we did not include it after block of instructions 1 the
program would not jump to the end of the switch selective block (}) and it would continue
executing the rest of the blocks of instructions until the first appearance of the break
instruction or the end of the switch selective block. This makes it unnecessary to include
curly brackets { } in each of the cases, and it can also be useful to execute the same block of
instructions for different possible values for the expression evaluated. For example:
switch (x) {
case 1:
case 2:
case 3:
cout<< "x is 1, 2 or 3";
break;
default:
cout<< "x is not 1, 2 nor 3";
}

III. Procedures:

Computer Fundamentals and Programming I Page 3


1. Encode the C++ program below.
2. Save as the file name indicated.
3. Compile and run the program by pressing Ctrl + F9.
4. Capture the output of the program by using snipping / print screen tool.
5. Discuss the output of the program.
6. Upload your work in E-Learning, filename: LASTNAME.DOC. (Title page and
Output/Analysis pages only).
7. Do not turn off your terminal. Programs encoded will be checked by your
instructor. Run all programs at one time for faster checking.

Computer Fundamentals and Programming I Page 4


Computer Fundamentals and Programming I Page 5
Computer Fundamentals and Programming I Page 6
Laboratory Exercise 4
CONDITIONAL switch STATEMENT

Direction: Demonstrate the corresponding output for each program in the boxes
below. Give your analysis and observation for each output.

PROGRAM OUTPUT ANALYSIS / OBSERVATION

1.

2.

Computer Fundamentals and Programming I Page 7


3.

Computer Fundamentals and Programming I Page 8


Machine Exercise 4
CONDITIONAL switch STATEMENT

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.

Computer Fundamentals and Programming I Page 9

You might also like