UNIVERSITY of MINDANAO
College of Engineering Education
EDP 101/L
Computer Programming Fundamentals
Laboratory Exercise # 3
Conditional Statements
if /else if
Student Name
(LN, FN MI)
Laboratory Rm No. Subject Code
Subject Teacher
Date Submitted
Score
Laboratory Exercise # 3
CONDITIONAL STATEMENTS
If/else if
Objectives:
At the conclusion of this laboratory exercise, the student should be
able to:
1. use basic mathematical operation to compute for grades
2. use if…else conditional structure in determining appropriate
grade remarks
3. use appropriate logical and relational operators to cover desired
range of grades
Materials:
1 computer set
C++ IDE
Introduction:
Conditional statement is a programming language statement
that decides an execution path based on whether some condition is
evaluated true or false.
In C++, there are two selections, or branch control structures: if
statements and the switch structure. For the if…else structure,
statements can be used to create a one-way selection, two-way
selection, and a multiple selection. On the other hand, a switch
structure is usually used in a menu drive program.
This laboratory exercise focuses mainly on the discussion of the
different methods in using the if statements control structure.
Relational Operators
Relational operators are used when dealing with comparison of
numeric and character values. Shown in Table 3.1 are the different
symbols and descriptions of each relational operator.
Symbol Description
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
Table 3.1 Relational Operators
In evaluating an expression with multiple relational operators a
hierarchy of relational operators is being used. Refer to Table 3.2 for
the hierarchy of relational operators.
Operator Symbol Precedence
> >= < <= Highest
== != Lowest
Table 3.2 Relational Operators Hierarchy
An expression of the form (num1 > num2) is called a relational
expression. Note that it does not assert that num1 is greater than
num2. It actually tests to see if this is true. Therefore, relational
expressions are Boolean. Their value must be either true or false.
One-way Selection
Syntax:
if (expression)
statement;
The expression in an if statement can be a relational expression
or a combination of relational and logical expressions. If the value of
the expression is true, the statement executes. If the value is false, the
statement does not execute and the computer goes on to the next
statement in the program.
Sample Program 3.1:
// This program prints "You Pass" if a student's average is 60
// And prints “A+” if student's average is greater than 95
#include <iostream>
using namespace std;
int main()
{
float average;
cout << "Input your average" << endl;
cin >> average;
if (average >= 60) // note the use of a relational operator
cout << "You Pass" << endl;
if (average > 95)
cout << "A+" << endl;
return 0;
}
In the example code above, if the input average is 90 then only
the first condition is true and executed. If the input average is less than
60, say 50, then none of the conditions are true and executed. An
average of 97 in this case will allow the two conditions to be evaluated
true and executed. Therefore, a trailing if statement could result to
the following: only one condition is being executed, all condition is
being executed, and no condition is being executed.
Two-way Selection
Syntax:
if (expression)
statement;
else
statement;
There are many programming situations in which you must choose
between two alternatives. To choose between two alternatives C++
provides the if…else statement.
If…else statement is a control that executes a block of code if a
Boolean expression evaluates to true. It uses the statement under the
keyword else if the above condition is false.
Unlike in Program 3.1 where there are different scenarios in
evaluation, the if…else statement will allow only one condition to be
true and executed. Example enhancement to Program 3.1 is shown
below:
if (average >= 60)
cout << "You Pass" << endl;
else
cout << "You Fail" << endl;
In every if/else statement the program can take only one of two
possible paths. Multiple statements can be handled using curly braces
in the same way as the if statement.
Multiple Selection: Nested if
Syntax:
if (condition1)
statement/s;
else if (condition2)
statement/s;
else if (condition3)
statement/s;
else
statement/s;
Some problems require the implementation of more than two
alternatives, for that you would need multiple selection paths.
Sample Program 3.2
Suppose we need to decide what kind of vacation to take based on a
yearly work bonus:
= if the bonus is less than $1,000, we set up a tent and eat steak in the
back yard
= if the bonus is less than $10,000 and greater than or equal to $1,000,
we go to Disney World
= if the bonus is $10,000, we go to Hawaii
We could code this using if/else if/else statement as follows:
if (bonus < 1000)
cout << "Another vacation on the lawn" << endl;
else if (bonus < 10000)
cout << "Off to Disney World!" << endl;
else if (bonus == 10000)
cout << "Lets go to Hawaii!" << endl;
else
{
cout << bonus << " is not a valid bonus" << endl;
cout << "Please run the program again with valid data"
<< endl;
} // Note the necessary use of the curly brackets here
Logical Operators
By using relational operators C++ programmers can create
relational expressions. It is also possible to combine truth values into
a single expression by using logical operators.
The syntax used by C++ for logical operators is the following:
Operator Description
&& AND
|| OR
! NOT
Table 3.3 Logical Operators
The NOT operator negates a single statement. For example, “it is
sunny” can be negated by “it is not sunny.” Another logical operators
are the OR and the AND which evaluates two or more statements. The
OR and AND logical operators allows the creation of a relational
expression from two or more relational expressions.
Consider x = 15, y = 7, and z= -5 for the example shown in the table
below:
Expression Evaluation
(x > y) && (y > z) TRUE
(x > z) && (z > y) FALSE
(x <= z) || (y == z) FALSE
(x <= z) || (y != x) TRUE
!(x >= y) FALSE
Table 3.4 Evaluation of Logical Operators
Let us now apply the logical operators in the following statement:
An individual is allowed to vote if he is of legal age, that is if he
is 18 yrs old and above; and has reside for at least 6 months. Take note
of the conjunction and, it means both statement must be true before
an individual be allowed to vote.
Let (age >= 18 yrs) be statement A and (residency >= 6 months)
be statement B. Both statements must be true, that means you must
be at least 18 years old and has reside in the locality for at least 6
months in order for you to vote.
In C++, the correct syntax would be:
if(age>=18 && residency>=6)
cout<< “ qualified voter ”;
else
cout<< “ not eligible to vote “ ;
Laboratory Task:
Create a program that computes the general average and display
the remarks of the corresponding grade. Implement using if…else or
laddered if/else if conditions.
Program Requirements:
1. The program will ask the user to input scores for examinations,
quizzes, laboratory exercises and laboratory project. Please refer
to figure 3.1 (sample screen display).
2. It shall only accept inputs ranging from 0 - 100.
3. A Base-15 grading system shall be used to compute the grades.
(raw score / no. of items) * 85 + 15;
4. Please refer to figure 3.2 for the assigned weights of each
activity.
5. After keying in all inputs, the Final Grade and Remarks will be
shown.
6. The program will allow to key in another set of inputs if an <Enter
Key> is pressed and exit for any other key (use ASCII code of
Enter Key, 13).
7. Display the remarks as output on the following given scale.
Please refer to figure 3.3.
Computation of Grades
Major Examinations (100 pts each) Quizzes (40 pts each)
First : _______ Qz # 1 : _____
Second: _______ Qz # 2 : _____
Third : _______ Qz # 3 : _____
Final : _______
Laboratory Exercises (50 pts each)
Exer # 1 : _______
Exer # 2 : _______
Exer # 3 : _______
Exer # 4 : _______
Laboratory Project (100 pts): ________
Gen. Av. : _________
Remarks : _________
Press <Enter Key> to Re-run the Program
Press <any key> to exit
Figure 3.1 Computations of Grades.
Weights Assignment
First Examination 10% Laboratory Exercise 10%
Second Examination 10% Quizzes 10%
Third Examination 10% Laboratory Project 20%
Final 30%
Figure 3.2 Weights assignment.
General Average Remarks
100 – 95 Excellent
94 - 90 Very Good
89 - 85 Good
84 – 80 Satisfactory
79 – 75 Fair
Below 75 Failed
Figure 3.3 Equivalent Remarks.
Questions:
1. What is the process used to compute the general average?
Answer: _____________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
2. What logical statement is used to identify which remarks of a
grade is shown?
Answer: _____________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
3. How many segments of the “if else” statement needed to show
appropriate remarks of the gen. average?
Answer: _____________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
4. What logical and relational operators where used to state
appropriate range of grades?
Answer: _____________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
5. Write the complete algorithm of the program.
Answer:
Observations:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Scoring Rubrics
Ratings
Parameters
3 2 1
Specifications Source code contains Source code lacks Source code is not
(30%) the complete details to necessary details to enough to run the
run the program run the program program correctly.
correctly. correctly.
(30) (20) (10)
Syntax Source code contains Source code Source code
(20%) no syntax error. contains 1 to 5 contains more than
syntax errors. 5 syntax errors.
(20) (14) (7)
Source code is well Minor issues such as Major issues are
Readability organized and easy to variable naming, causing the codes to
(20%) understand. variable utilization, be not readable.
etc. are observed.
(20) (14) (7)
Screen Output Source code is well Source code allows Source code does
(10%) organized and easy to the required screen not meet the
understand. output to be required screen
displayed correctly output to be
with 1-3 errors displayed correctly.
found.
(10) (6) (3)
The documentation is The documentation The documentation
Documentation well written and clearly lacks some didn’t satisfy all the
(10%) explained all the information and questions that were
questions given in the some mistakes are given in the
laboratory exercise. found in the laboratory exercise.
questions.
(10) (6) (3)