Beruflich Dokumente
Kultur Dokumente
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
Question 2
Who is the inventor of Boolean algebra?
Answer
Question 3
Define the relational operator.
Answer
Question 4
List SIX (6) C++ symbols for relational operators.
Answer
Question 5
What is the difference between operators ‘=’ and ‘==’?
Answer
Question 6
Give the meaning of the following simple Boolean expressions:
Answer
8 != 8
5 == 5
Question 7
Give the value TRUE or FALSE for the following simple Boolean expressions:
i == 4
j >= 5
j != i
i < j + 1
Question 8
What is logical operator?
Answer
Question 9
List THREE (3) C++ symbols for logical operators.
Answer
Question 10
What is the difference between operator ‘&&’ and ‘||’?
Answer
Question 11
Give the value TRUE or FALSE for the following compound Boolean expressions (NOT):
Answer
A !A
TRUE
FALSE
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 FALSE
FALSE TRUE
FALSE FALSE
Question 13
Give the value TRUE or FALSE for the following compound Boolean expression:
Answer
x >= y && z == 3
y < 15 || !y > 0
Question 14:
Write the syntax statements for the following flow chart.
true
condition statement 1
false
Answer
Question 15
Write the program statements for the following flow chart.
true
Price ≥ 50? Discount = 0.2
false
Answer
Question 16
What is compound statement?
Answer
Question 17
What is the symbol used for compound statements?
Answer
Question 18
Write the program statements for the following flow chart.
true
Price ≥ 50? Discount = 0.2
false
NewPrice = Price – (Price *Discount)
Answer
Question 19
What is the purpose of two-way selection?
Answer
Question 20
Draw a flow chart for a two-way selection form structure.
Answer
Question 21
Write the syntax statement of general form for two-way selection structure.
Answer
Question 22
Rewrite the following codes into two-way selection structure.
Answer
Question 23
Rewrite the following codes into two-way selection structure.
Answer
Question 24
Rewrite the following codes into two-way selection structure.
Answer
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
Question 26
Write the program statements based on the flow chart.
false true
Display Number can be
divided by 2? Display
“odd number” “even number”
Answer
Question 27
Write the program statements based on the flow chart.
false true
Discount =2% x Sale Sale >= 5000? Discount = 5% x Sale
Display
Answer
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
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?
a. Run the program again. What is the program output if the mark is 50?
b. Run the program again. What is the program output if the mark is 45?
Exercise 2
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?
b. Run the program again. What is the program output if the mark is 45?
Exercise 3
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?
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?
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.
b. Run the program again. What is the mark for the program to display only the
following message?
Exercise 5
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?
b. Edit the program to detect a maximum value?
Exercise 6
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.
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?
false
PriceAfterDiscount = Price – Discount
Display
Display
“Thank you”
Exercise 8
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?
Exercise 9
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?
Exercise 10
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
Display
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:
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.
Student ID :
Date :
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:
Student ID :
Date :
Name :
Student ID :
Date :
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:
Student ID :
Date :
Name :
Student ID :
Date :
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:
Student ID :
Date :
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:
Student ID :
Date :
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:
Student ID :
Date :
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:
Student ID :
Date :