Professional Documents
Culture Documents
SELECTION STRUCTURE
(Part I)
Learning Outcomes
After completing this lab, you will be able to:
o use Boolean expressions
o use the relational and logical operators
o know the structure of one-way and two-way selection
o implement decisions using one-way and two-way selection
A. PRE-LAB ACTIVITIES
Question 1
Name TWO (2) values for Boolean expressions?
Answer
Two values for Boolean expressions are True or False
Question 2
Who is the inventor of Boolean algebra?
Answer
Question 3
Define the relational operator.
Answer
Relational operator is used to test expression that controls the program flow. This is
known as Boolean expression as they created a value when answered.
Question 4
List SIX (6) C++ symbols for relational operators.
Answer
< Less than
> More than
<= Less than or equal
>= More than or equal
== Equal
!= or <> Not equal
Question 5
What is the difference between operators ‘=’ and ‘==’?
Answer
Difference between “=” and “==” are :
= ==
Assigment Operator Comparison Operator
Used to assigning value Comparison between 2 values
Question 6
Give the meaning of the following simple Boolean expressions:
Answer
Boolean Meaning
Expression
9 < 16 9 is less than 16
8 != 8 8 is not equal to 8
5 == 5 5 is equal to 5
Question 7
Give the value TRUE or FALSE for the following simple Boolean expressions:
Answer
i == 4 TRUE
j >= 5 TRUE
j != i TRUE
i < j + 1 FALSE
Question 8
What is logical operator?
Answer
Logical operator is a symbol or word used to connect two or more expressions.
Question 9
List THREE (3) C++ symbols for logical operators.
Answer
&&
||
!
Question 10
What is the difference between operator ‘&&’ and ‘||’?
Answer
If applied to boolean values, the && operator only returns true when both of its operands
are true (and false in all other cases), while the || operator only returns false when both
of its operands are false (and true in all other cases).
Question 11
Give the value TRUE or FALSE for the following compound Boolean expressions (NOT):
Answer
A !A
TRUE FALSE
FALSE TRUE
Question 12
Give the value TRUE or FALSE for the following compound Boolean expressions (AND,
OR):
Answer:
A B A && B A || B
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
FALSE TRUE FALSE TRUE
FALSE FALSE FALSE FALSE
Question 13
Give the value TRUE or FALSE for the following compound Boolean expression:
Answer
true
condition statement 1
false
Answer
If condition is true, execute statement 1. If the condition is false, end the program.
If (condition)
cout << statement1;
Return 0;
Question 15
Write the program statements for the following flow chart.
true
Price ≥ 50? Discount = 0.2
50?
false
Answer
if (price >= 50)
discount = 0.2;
return 0;
Question 16
What is compound statement?
Answer
A compound statement is a sentence that consists of two or more statements separated
by logical connectors.
Question 17
What is the symbol used for compound statements?
Answer
block
Question 18
Write the program statements for the following flow chart.
true
Price ≥ 50? Discount = 0.2
50?
false
NewPrice = Price – (Price *Discount)
Answer
if (Price >= 50)
{
discount=0.2;
newPrice = Price – (Price*Discount);
}
Return 0;
Question 19
What is the purpose of two-way selection?
Answer
Two-way selection used to resolve a binary expression, and then executing a set of
commands depending on whether the response was true or false
Question 20
Draw a flow chart for a two-way selection form structure.
Answer
false true
condition
Statement2 Statement1
end
Question 21
Write the syntax statement of general form for two-way selection structure.
Answer
if (condition = 0)
cout<<statement1;
else
cout << statement2;
Return 0;
Question 22
Rewrite the following codes into two-way selection structure.
Answer
if (mark >= 50)
cout << “PASSED” << endl;
else
cout << “failed” << endl;
return 0;
Question 23
Rewrite the following codes into two-way selection structure.
Answer
Return 0;
Question 24
Rewrite the following codes into two-way selection structure.
Answer
if (income <= 1000)
tax = income * 0.0125;
else
tax = income * 0.0225;
return 0;
Question 25
Rewrite the following codes into two-way selection structure.
if (year > 5 )
{
dividend = investment * 6.5;
balance = investment + dividend;
}
if (year <= 5)
{
dividend = investment * 3.5;
balance = investment + dividend;
}
cout << “The balance in your account is ” << balance << endl;
Answer
if (year > 5)
{
dividend = investment * 6.5;
balance = investment + dividend;
}
else
{
dividend = investment * 3.5;
balance = investment + dividend;
}
cout << “The balance in your account is “ << balance << endl;
Question 26
Write the program statements based on the flow chart.
Answer
if (number%2 == 0)
return 0;
Question 27
Write the program statements based on the flow chart.
false true
Discount =2% x Sale Sale >= 5000? Discount = 5% x Sale
Display
Sale after Discount
Answer
return 0;
Question 28
Write the following sentences to program statements.
If the payment is between RM5,000 to RM10,000, displays a message that the customer
will be given a free air ticket to Langkawi. Otherwise displays a message that the
customer will only be given a free lunch in Quality Hotel.
Answer
else
cout << “You will be given a free lunch in Quality Hotel”;
return 0;
B. LAB ACTIVITIES
Exercise 1
main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
cout << “PASSED” << endl;
}
Program 5.1
a. Compile and run the program. What is the program output if the mark is 60?
PASSED
a. Run the program again. What is the program output if the mark is 50?
PASSED
b. Run the program again. What is the program output if the mark is 45?
Edit your program according to the following program statements. Save your program
as lab5_prog2.cpp.
main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
cout << “PASSED” << endl;
cout << “Thank you for using this program.” << endl;
}
Program 5.2
a. Compile and run the program. What is the program output if the mark is 50?
PASSED
b. Run the program again. What is the program output if the mark is 45?
Edit your program according to the following program statements. Save your program
as lab5_prog3.cpp.
main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
{
cout << “PASSED” << endl;
cout << “Thank you for using this program.” << endl;
}
}
Program 5.3
a. Compile and run the program. What is the program output if the mark is 50?
PASSED
b. Run the program again. What is the program output if the mark is 45?
c. Compare Program 5.2 and Program 5.3. What is the difference between these
two programs?
The difference is the program 5.2 doing a two-way selection which tells the user thank
you for using the program if the mark is below 50. While 5.3 using a one-way selection
which give the answer to whom have mark above 50 and ends if the user have marks
below 50.
Exercise 4
Edit your program according to the following program statements. Save your program
as lab5_prog4.cpp.
main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark < 50)
{
cout << “FAIL” << endl;
cout << “You have to repeat this subject.” << endl;
}
cout << “Thank you for using this program.” << endl;
}
Program 5.4
a. Compile and run the program. What is the mark for the program to
display the following messages?
FAIL
You have to repeat this subject.
Thank you for using this program.
Mark < 50
b. Run the program again. What is the mark for the program to display only
the following message?
Mark >= 50
Exercise 5
Type the following program statements. Save your program as lab5_prog5.cpp.
main()
{
int no1, no2, min;
cout << “Enter the first value : ”;
cin >> no1;
cout << “Enter the second value : ”;
cin >> no2;
if (no1 < no2)
min = no1;
if (no2 <= no1)
min = no2;
cout << “The lower value is ” << min << endl;
}
Program 5.5
a. Compile and run the program. What is the output if the value for no1 is 5 and
no2 is 6?
Exercise 6
Type the following program statements. Save your program as lab5_prog6.cpp.
main()
{
float basicSalary;
const float LIMIT = 1500;
cout << “Enter your basic salary in RM : ”;
cin >> basicSalary;
if (basicSalary >= LIMIT)
cout << “You are able to apply for credit card.” << endl;
}
Program 5.6
a. Compile and run the program. What is the value of basic salary that allows a
customer to apply for a credit card?
a. Edit the program to display the following messages if the basic salary is more
than or equal to 1500:
Congratulations!
You are able to apply for credit card.
Please contact our nearest branch for application.
Thank you for using this program.
float basicSalary;
const float LIMIT = 1500;
cout << "Enter your basic salary in RM : ";
cin >> basicSalary;
if (basicSalary >= LIMIT)
{
cout << "Congratulations!";
cout << "\nYou are able to apply for credit card.";
cout << "\nPlease contact our nearest branch for application";
cout << "\nThank you for using this program.";
}
else
cout << "Thank you for using this program.";
Exercise 7
Type a program based on the following flow chart. Your program is able to read price
from the keyboard. Save your program as lab5_prog7.cpp.
true
Discount = 5 % x Price
Price ≥ 1000?
50?
false
PriceAfterDiscount = Price – Discount
Display
“The price after discount is….”
Display
“Thank you”
return 0;
Exercise 8
Type the following program statements. Save your program as lab5_prog8.cpp.
main()
{
float bill;
cout << “Enter the water bill amount : ”;
cin >> bill;
if (bill >= 350)
cout << “You will receive a 1.5% discount.” << endl;
else
cout << “You will receive a 0.5% discount.” << endl;
}
Program 5.8
a. Compile and run the program. What is the program output if the bill is 350?
b. Run the program again. What is the program output if the bill is 349?
Edit your program according to the following program statements. Save your program
as lab5_prog9.cpp.
main()
{
float bill, discount, payment;
cout << “Enter the water bill amount : ”;
cin >> bill;
if (bill >= 500 && bill < 1000)
{
cout << “You will receive a 1.5% discount.” << endl;
discount = bill * 1.5;
}
else
{
cout << “You will receive a 0.5% discount.” << endl;
discount = bill * 0.5;
}
payment = bill – discount;
cout << “You have to pay only RM ” << payment << endl;
}
Program 5.9
a. Compile and run the program. What is the program output if the bill is 400?
b. Run the program again. What is the bill amount if the customer wants to have
1.5% discount?
main()
{
int no1, no2, min;
cout << “Enter the first value : ”;
cin >> no1;
cout << “Enter the second value : ”;
cin >> no2;
if (no1 < no2)
min = no1;
if (no2 <= no1)
min = no2;
cout << “The lower value is ” << min << endl;
}
Program 5.10
a. Rewrite the program by modifying the program into two-way selection. Save
your program as lab5_prog10.cpp.
Type a program based on the following flow chart. Your program should be able to read
price from the keyboard. Save your program as lab5_prog12.cpp.
Price after Charge = Price + Charge Price after Charge = Price + Charge
C C
Display
“The price after charge is….”
else
{
charge = 0.05* price;
PriceAfterCharge = price + charge;
}
a. Your program is able to read two inputs from the keyboard, which are the amount
of investment and profit.
b. If the investment is more than or equal to RM50,000 and the profit is more than
RM20,000, then the following message is displayed:
int main()
{
int investment, profit;
else
}
Exercise 13
a. Your program is able to read the amount of sale for a sale executive.
b. If the sale is more than RM10,000, then the commission will be 5% of the sale.
Otherwise the commission is only 3% of the sale.
d. Your program is also able to display the amount of commission to the sale
executive.
int main()
{
else
}
C. POST-LAB ACTIVITIES
Question 1
YDL Nation Bank wants to develop a program that can help the customer to calculate the
monthly installment. The program should be able to receive two inputs from the customer,
which are the amount of loan in Ringgit Malaysia and year of installment. Based on these two
inputs, the program should therefore be able to calculate the monthly installment. The table for
the interest calculation is provided as follows:
int main()
{
float loan, installment, totalLoan, interest;
int year;
cout << “Enter the amount of loan that you are taking : “;
cin >> loan;
if (year <5)
{
interest = (0.045 * loan);
totalLoan = loan + interest;
installment = totalLoan/(year*12);
}
else
{
interest = (0.065 * loan);
totalLoan = loan + interest;
installment = totalLoan/(year*12);
}
}
Question 2
Nova Hypermarket in your town wants to organize an annual sale. The owner of the
hypermarket wants you to help him to develop a program to help the cashier to calculate and
display the payment after discount. The table for the discount is provided as follows:
int main()
{
else
discount = 0;
}
Question 3
SSY Co Ltd is one of the finance companies. This company wants you to develop a program to
help up in calculating the bonus for its employees. The bonus will be calculated based on the
performance mark of the employee. Your program should be able to receive performance mark
from the user and display the bonus to the user as well. The table for the bonus is provided as
follows:
int main()
{
int perform, bonus;
}
Question 4
YDL Nation Bank wants to do some modification to their program so that it can help the
customer to calculate the monthly installment by using new condition. The program however is
still able to receive two inputs from the customer, which are the amount of loan in Ringgit
Malaysia and year of installment. Based on these two inputs, the program is able to calculate
the monthly installment. The table for the interest calculation is provided as follows:
int main()
{
float loan, installment, totalLoan, interest;
int year;
cout << “Enter the amount of loan that you are taking : “;
cin >> loan;
}
Question 5
Nova Hypermarket in your town wants to organize another annual sale. The owner of the
hypermarket wants you to help him to modify the program to help the cashier to calculate and
display the payment after discount based on new conditions The table for the discount is
provided as follows:
int main()
{
}
Question 6
SSY Co Ltd is one of the finance companies. This company wants you to develop a program to
help up in calculating the bonus for its employees. The bonus calculated based on the sales of
the employee. Your program is able to receive sales in Ringgit Malaysia from the user and
display the bonus to the user. The table for the bonus is provided as follows:
int main()
{
int sale, bonus;