You are on page 1of 32

Lab 5

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

George Boole is the inventor of Booleans algebra

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:

For example: 5 < 10  5 is less than 10

Answer

Boolean Meaning
Expression
9 < 16 9 is less than 16

8 != 8 8 is not equal to 8

3.5 > 6.8 3.5 is more than 6.8

6.9 <= 8.5 6.9 is less than or equal to 8.5

5 == 5 5 is equal to 5
Question 7
Give the value TRUE or FALSE for the following simple Boolean expressions:

Assume that i = 4 and j = 6.

Answer

Boolean Expression Value (TRUE or FALSE)


2 == 3 FALSE

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:

Assume that x = 5, y = 6 and z = 7

Answer

BOOLEAN EXPRESSION VALUE (TRUE or FALSE)


x < y && z > y TRUE

x <= z && x > 0 TRUE

x >= y && z == 3 FALSE

y < 15 || !y > 0 TRUE

x > y || z > y && !z <= 0 FALSE


Question 14:
Write the syntax statements for the following flow chart.

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.

if (mark >= 50)


cout << “PASSED” << endl;
if (mark < 50)
cout << “FAILED” << endl;

Answer
if (mark >= 50)
cout << “PASSED” << endl;
else
cout << “failed” << endl;

return 0;

Question 23
Rewrite the following codes into two-way selection structure.

if (price >= 2000)


interest = 3.5;
if (mark < 2000)
interest = 2.5;

Answer

if ( price >= 2000)


interest = 3.5;
else
interest = 2.5;

Return 0;
Question 24
Rewrite the following codes into two-way selection structure.

if (income <= 1000 )


tax = income * 0.0125;
if (income > 1000)
tax = income * 0.0225;
incomeAfterTax = income – tax;

Answer
if (income <= 1000)
tax = income * 0.0125;
else
tax = income * 0.0225;

incomeAfterTax = income – tax;

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.

Display false true


Number can be divided by 2? Display
“odd number”
“even number”

Answer
if (number%2 == 0)

cout << “true”


else
cout << “false”

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

Sale after Discount


Sale after Discount = Sale - Discount
= Sale - Discount

Display
Sale after Discount

Answer

If (sale >= 5000)


{
discount = 0.05 * sale;
totalSale = sale – discount;
}
else
{
discount = 0.02 * sale;
totalSale = sale – discount;
}

cout << totalSale;

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

If (payment >= 5000 && payment <= 10000)


cout << “You will be given a free air ticket to Langkawi”;

else
cout << “You will be given a free lunch in Quality Hotel”;

return 0;
B. LAB ACTIVITIES
Exercise 1

Type the following program statements. Save your program as lab5_prog1.cpp.

//Program Name: lab5_prog1.cpp


//This program displays a status based on the mark.
#include <iostream.h>

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?

The program ends.


Exercise 2

Edit your program according to the following program statements. Save your program
as lab5_prog2.cpp.

//Program Name: lab5_prog2.cpp


//This program displays messages based on the mark range.
#include <iostream.h>

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?

Thank you for using this program.


Exercise 3

Edit your program according to the following program statements. Save your program
as lab5_prog3.cpp.

//Program Name: lab5_prog3.cpp


//This program displays messages based on the mark.
#include <iostream.h>

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?

The program ends.

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.

//Program Name: lab5_prog4.cpp


//This program displays messages based on the mark.
#include <iostream.h>

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?

Thank you for using this program.

Mark >= 50

Exercise 5
Type the following program statements. Save your program as lab5_prog5.cpp.

//Program Name: lab5_prog5.cpp


//This program is to display the lower value between 2 numbers
#include <iostream.h>

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?

The lower value is 5

b. Edit the program to detect a maximum value?

int no1, no2, max;


cout << "Enter the first value : ";
cin >> no1;
cout << "Enter the second value : ";
cin >> no2;
if (no1 > no2)
max = no1;
if (no2 >= no1)
max = no2;
cout << "The max value is " << max << endl;

Exercise 6
Type the following program statements. Save your program as lab5_prog6.cpp.

//Program Name: lab5_prog6.cpp


//This program is to identify whether a customer can apply for
//credit card or not
#include <iostream.h>

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?

basicSalary > 1500

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.

Otherwise only the following statement will be displayed.

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”

If (price >= 1000)


{
Discount = 0.05 * Price;
PriceAfterDiscount = Price – Discount;
cout << “The price after discount is…” << PriceAfterDiscount;
}

cout << “Thank you”;

return 0;

Exercise 8
Type the following program statements. Save your program as lab5_prog8.cpp.

//Program Name: lab5_prog8.cpp


//This program displays a message based on the water bill amount.
#include <iostream.h>

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?

You will receive a 1.5% discount.

b. Run the program again. What is the program output if the bill is 349?

You will receive a 0.5% discount.


Exercise 9

Edit your program according to the following program statements. Save your program
as lab5_prog9.cpp.

//Program Name: lab5_prog9.cpp


//This program displays a message based on the water bill amount.
#include <iostream.h>

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?

You will receive a 0.5% discount.


You have to pay only RM 200

b. Run the program again. What is the bill amount if the customer wants to have
1.5% discount?

bill >= 500 && bill <1000


Exercise 10

Below is an executable program by using one-way selection structure.

//Program Name: lab5_prog10.cpp


//This program is to display the lower value
#include <iostream.h>

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.

int no1, no2, min;


cout << "Enter the first value : ";
cin >> no1;
cout << "Enter the second value : ";
cin >> no2;
if (no1 < no2)
min = no1;
else
min = no2;
cout << "The lower value is " << min << endl;

b. What is the output if the value for no1 is 5 and no2 is 6?

The lower value is 5


Exercise 11

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.

Charge = 5 % x Price false true Charge = 3 % x Price


Price ≥ 1000?
50?

Price after Charge = Price + Charge Price after Charge = Price + Charge
C C

Display
“The price after charge is….”

if (price >= 1000)


{
charge = 0.03 * price;
PriceAfterCharge = price = charge;
}

else
{
charge = 0.05* price;
PriceAfterCharge = price + charge;
}

cout << “The price after charge is “ << PriceAfterCharge;


Exercise 12

You are given the following requirements:

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:

This investment is worthy to continue.

c. Otherwise displays the following message

This investment is a waste of money.

Write a program based on the above requirements. Save your program as


lab5_prog13.cpp.

int main()
{
int investment, profit;

cout << “ Please enter your investment : “;


cin >> investment;
cout << “\nPlease enter your profit : “;
cin >> profit;
If ( investment >= 50000 && profit > 20000)

cout << “\nThis investment is worthy to continue”;

else

cout << “\nThis investment is a waste of money.”;

}
Exercise 13

You are given the following requirements:

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.

c. Your program is able to calculate the amount of commission by multiply the


percentage of commission with sale.

d. Your program is also able to display the amount of commission to the sale
executive.

Write a program based on the above requirements. Save your program as


lab5_prog14.cpp.

int main()
{

float sale, commission;

cout << “Please enter the sale amount : “;


cin >> sale;

if (sale > 10000)

commission = 0.05 * sale;

else

commission = 0.03 * sale;

cout << “\nThe commission for you are : “ << commission;

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

Year Interest (%)


<5 4.5
≥5 6.5

The formulas for the monthly installment calculation are as follows:

Interest (RM) = Interest (%) x Amount of Loan


Total Loan with Interest = Amount of Loan + Interest (RM)
Monthly Installment = Total Loan with Interest / (Year of Installment x 12)

int main()
{
float loan, installment, totalLoan, interest;
int year;

cout << “Enter the amount of loan that you are taking : “;
cin >> loan;

cout << “\nEnter the year of installment : “;


cin >> year;

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

cout << “Your monthly installment are : “ << installment;

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

Amount (RM) Discount (%)


≥ 300 5
< 300 No Discount

The formulas for the calculation are as follows:

Discount (RM) = Discount (%) x Amount (RM)


Payment After Discount = Amount (RM) – Discount (RM)

int main()
{

double amount; total, discount;

cout << “Please enter the payment amount : “;


cin >> amount;

if (amount >= 300)


{
discount = 0.05 * amount;
total = amount – discount;
}

else

discount = 0;

cout << “Total are : “ << total;

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

Performance Mark Bonus (RM)


6 ≤ Mark ≤ 10 2000
Mark < 6 1000

int main()
{
int perform, bonus;

cout << “Please enter performance mark of the employee : “;


cin >> perform;

if (perform >= 6 && perform <= 10)


bonus = 2000;

else if (mark < 6)


bonus = 1000;

cout << “The employee bonus are : “ << 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:

Condition Interest (%)


5 ≤ Year ≤ 10 6.7
3 ≤ Year < 5 5.5

The formulas for the monthly installment calculation are as follows:

Interest (RM) = Interest (%) x Amount of Loan


Total Loan with Interest = Amount of Loan + Interest (RM)
Monthly Installment = Total Loan with Interest / (Year of Installment x 12)

int main()
{
float loan, installment, totalLoan, interest;
int year;

cout << “Enter the amount of loan that you are taking : “;
cin >> loan;

cout << “\nEnter the year of installment : “;


cin >> year;

if (year >=5 && year <= 10)


{
interest = (0.067 * loan);
totalLoan = loan + interest;
installment = totalLoan/(year*12);
}
else if ( year >= 3 && year < 5)
{
interest = (0.055 * loan);
totalLoan = loan + interest;
installment = totalLoan/(year*12);
}

cout << “Your monthly installment are : “ << installment;

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

Amount (RM) Discount (%)


300 ≥ Amount ≥ 1000 5.5
Amount > 1000 6.5

The formulas for the calculation are as follows:

Discount (RM) = Discount (%) x Amount (RM)


Payment After Discount = Amount (RM) – Discount (RM)

int main()
{

double amount; total, discount;

cout << “Please enter the payment amount : “;


cin >> amount;

if (amount >= 300 && amount <= 1000)


{
discount = 0.55 * amount;
total = amount – discount;
}

else if (amount > 1000)


{
discount = 0.065 * amount;
total = amount – discount;
}
cout << “Total are : “ << total;

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

Sales (RM) Bonus (RM)


50,000 ≤ Sales ≤ 100,000 5000
35,000 ≤ Sales < 50,000 3000

int main()
{
int sale, bonus;

cout << “Please enter sales of the employee : “;


cin >> sale;

if (sale >= 50000 && perform <= 100000)


bonus = 5000;

else if (sale >= 35000 && sale < 50000)


bonus = 3000;

cout << “The employee bonus are : “ << bonus;

You might also like