Lab Activity 6
Lab Activity 6
CPE 035
Programming Logic and Design
Laboratory Manual
Activity No. 6
Jump and Switch Statements
Objectives: 1. Identify how jumps and switch statement is used in programming
2. Make a program using jump and switch statements
Jump statements
Jump statements allow altering the flow of a program by performing jumps to
specific locations.
switch (expression)
case constant1:
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
group-of-statements-1;
break;
case constant2:
group-of-statements-2;
break;
default:
default-group-of-statements
Finally, if the value of expression did not match any of the previously specified
constants (there may be any number of these), the program executes the
statements included after the default: label, if it exists (since it is optional).
Both of the following code fragments have the same behavior, demonstrating the
if-else equivalent of a switch statement:
switch (x) { if (x == 1) {
break; else if (x == 2) {
break;
else {
default:
cout << "value of x unknown";
cout << "value of x
unknown";
}
}
The switch statement has a somewhat peculiar syntax inherited from the early
times of the first C compilers, because it uses labels instead of blocks. In the
most typical use (shown above), this means that break statements are needed
after each group of statements for a particular label. If break is not included, all
statements following the case (including those under any other labels) are also
executed, until the end of the switch block or a jump statement (such as break) is
reached.
If the example above lacked the break statement after the first group for case
one, the program would not jump automatically to the end of the switch block
after printing x is 1, and would instead continue executing the statements in case
two (thus printing also x is 2). It would then continue doing so until
a break statement is encountered, or the end of the switch block. This makes
unnecessary to enclose the statements for each case in braces {}, and can also
be useful to execute the same group of statements for different possible values.
For example:
1 switch (x) {
2
3 case 1:
4
5 case 2:
6
7 case 3:
8
9 cout << "x is 1, 2 or 3";
break;
default:
}
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
Notice that switch is limited to compare its evaluated expression against labels
that are constant expressions. It is not possible to use variables as labels or
ranges, because they are not valid C++ constant expressions.
To check for ranges or values that are not constant, it is better to use
concatenations of if and else if statements.
Procedure:
2. Run the program and place the output on the space provided
3. Write your observations on the output or how and why you arrive that output.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
4. Create a C++ program to find the maximum between two numbers using the
switch statement.
5. Write a C++ program to print day of week name using switch case.
6. Write a C++ program to check whether a number is even or odd using switch
case.
Procedure 4 Procedure 5
Procedure 6
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department
Observation:
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Conclusion
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Rubric
1 2 3 4
Student spent too Student spent too Student spent an Student spent an
much time and/or much time and/or adequate amount of adequate amount of
Adequate Time too little time on too little time on time on computer time on computer
Spent on Activity entire computer parts of computer lab activity to ensure lab activity to ensure
lab activity. lab activity. good results. the best results.
Student put little to Student put little Student put a good Student put a great
no effort towards effort towards amount of effort deal of effort
Effort computer lab computer lab towards computer towards computer
activity activity. lab activity. lab activity.
Student completed Student completed Student completed Student completed
less than 1/2 of about 1/2 of the about 80% of the all of the computer
Completion of the computer lab computer lab computer lab activity lab activity by the
Task activity by the due activity by the due by the due date. due date.
date. date.
Responses and Responses and Responses and Responses and
information given information given information given information given
Reasonable are entirely are unreasonable are reasonable are very reasonable
Response and unreasonable in some areas of throughout most of throughout all of the
Information throughout the the activity. the activity. activity.
activity.
Neatness, Responses and Responses and Responses and Responses and
Readability, and information given information given information given information given
Legibility are entirely are unreadable are neat, readable, are very neat,
unreadable and and illegible and legible readable, and
illegible throughout most of throughout most of legible throughout all
throughout the the activity. the activity. of the activity.
activity.