0% found this document useful (0 votes)
5 views

Lab Activity 6

For CPE

Uploaded by

jansteff2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Activity 6

For CPE

Uploaded by

jansteff2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PHINMA – Cagayan de Oro College

College of Engineering and Architecture


Computer Engineering Department

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

 Materials: Computer & C/C++ programming language software tool

Jump statements
Jump statements allow altering the flow of a program by performing jumps to
specific locations.

The break statement


break leaves a loop, even if the condition for its end is not fulfilled. It can be used
to end an infinite loop, or to force it to end before its natural end. For example,
let's stop the countdown before its natural end:

The goto statement


goto allows to make an absolute jump to another point in the program. This
unconditional jump ignores nesting levels, and does not cause any automatic
stack unwinding. Therefore, it is a feature to use with care, and preferably within
the same block of statements, especially in the presence of local variables.

The destination point is identified by a label, which is then used as an argument


for the goto statement. A label is made of a valid identifier followed by a colon (:).

goto is generally deemed a low-level feature, with no particular use cases in


modern higher-level programming paradigms generally used with C++. But, just
as an example, here is a version of our countdown loop using goto:

Another selection statement: switch


The syntax of the switch statement is a bit peculiar. Its purpose is to check for a
value among a number of possible constant expressions. It is something similar
to concatenating if-else statements, but limited to constant expressions. Its most
typical syntax is:

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

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


equivalent to constant1; if it is, it executes group-of-statements-1 until it finds
the break statement. When it finds this break statement, the program jumps to
the end of the entire switch statement (the closing brace).

If expression was not equal to constant1, it is then checked against constant2. If


it is equal to this, it executes group-of-statements-2 until a break is found, when it
jumps to the end of the switch.

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 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"; }


PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Computer Engineering Department

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:

cout << "x is not 1, 2 nor 3";

}
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:

1. Let's have a look at the following code below

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.

Instructor: Percentage Total


Name: Subject
Code

You might also like