The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by sariah.rajuli, 2021-12-02 22:15:16

Selection Control Structure

Selection Control Structure

Selection
Control Structure

Chapter 3

Selection/Decision control statements

• In Python, the selection statements are also known as Decision control
statements or branching statements.

• The selection statement allows a program to test several conditions and
execute instructions based on which condition is true.

• Some Decision Control Statements are:

• Simple if
• if-else
• nested if
• if-elif-else

Boolean Expression

• In order to allow the computer to make the decision, it needs a
Boolean expression that helps the computer to make decision,
whether true or false.

• In order to evaluate a comparison between two expressions, we
can use the Relational operators or Logical operators.

3

Relational Operator Name Description
Operators
== Equal to Checks whether two operands are
The six common equal
relational operators
available in Python != Not equal to Checks whether two operands are
not equal

Checks whether the left side
> Greater than operand is greater than the right

side operand

Checks whether the left side
< Less than operand is less than the right side
operand

>= Greater than Checks whether the left side
or equal to operand is either greater or equal
to the right side operand

<= Lesser than Checks whether the left side
or equal to operand is either less than or equal
to the right side operand

Logical Operator What it means Example
Operators and True if both are true x and y
or True if at least one is true x or y
There are three logical operators not True only if false not x
that are used to compare values.
They evaluate expressions and To understand how logical operators work,
return either True or False. These let’s evaluate these three expressions:
operators are and, or, and not.

Exercise 1(Logical and relational operator)

write a python code for the system below:

A scholarship is provided for the student in one institution. The student will be
given the scholarship based on the conditions below:
• Sibling more than 6
• Parent’s income less than RM2000
The number of sibling and parent’s income will be as an input. If both conditions
are true then the message “Qualify to get scholarship” will be displayed. Otherwise
message “Not qualify to get scholarship” will be displayed.

if (sibling>5 and P_income<2000)

6

Solution for exercise 1

Simple if

• If statements are control flow statements that help us to run a particular
code, but only when a certain condition is met or satisfied. A simple if only
has one condition to check.

if-else

• The if-else statement evaluates the condition and will execute
the body of if if the test condition is True, but if the condition is
False, then the body of else is executed.

Exercise 2

• Write a code to determine whether a person is a child or an
adult based on the age entered by the user. If the age is less or
equal to 18, display ‘CHILD’, otherwise display ‘ADULT’.

Exercise 2 (solution)

nested if

• Nested if statements are an if
statement inside another if
statement.

Python Nested if Example

• '''In this program, we input a number check if the number is
positive or negative or zero and display an appropriate
message. We use nested if statement'''

We can have a if...elif...else statement
inside another if...elif...else statement. This
is called nesting in computer
programming.

Any number of these statements can be
nested inside one another. Indentation is
the only way to figure out the level of
nesting. They can get confusing!

Exercise 2 (Nested if)

Write a python code for the system below using the nested if:

A scholarship is provided for the student in one institution. The student will be
given the scholarship based on the conditions below:
• Sibling more than 5
• Parent’s income less than RM2000
The number of sibling and parent’s income will be as an input. If both conditions
are true then the message “Qualify to get scholarship” will be displayed. Otherwise
message “Not qualify to get scholarship” will be displayed.

if (sibling>5 and P_income<2000)

14

Exercise 2(Nested if - solution)

if-elif-else

• The if-elif-else statement is used to conditionally
execute a statement or a block of statements.

• The elif is short for else if. It allows us to check for
multiple expressions.

Exercise 3

• Calculate and display the fee for a bus passengers. The fee for
an adult (>12 years old) is RM15 and child is RM8 each.
However, adult who is 50 years old and above will get 10%
discount from the adult fee. The system should ask age from
the user as an input and display fee for a passenger.

Exercise 3 (Solution)

Exercise 4

• Rafflesia Resort offers promotional Family Day
package as follows:

Participant Price per head (RM)
1 – 100 50
>100 and <250 45

◼ Write a Python program to:
a. prompt a user to input the number of
participants.
b. calculate and display the total price

19

Exercise 4 Output example

Number of participant? : 10

Total price : RM500

20

Exercise 4 (Solution)

Exercise 5

• Rafflesia Resort offers promotional Family Day
package as follows:

Participant Price per head (RM)
1 – 100 50
>100 and <250 45

◼ Write a C++ program to
a. prompt a user to input number of participants.
b. Calculate the total price
c. Calculate the total tax and total nett charge
(subjected to 13% government tax)

22

Exercise 5 Output example

Number of participant? : 10

Total price : RM500

Government tax (13%) : RM65

Nett price : RM565

23

Exercise 5 (Solution)

Exercise 6

• Rafflesia Resort offers promotional Family Day package as follows:

Package Code ParticiPpaantckage Name ParticiPpricae pnerthead (RM)Price(pReMr)head

1 – 100 1 – 100 50 50

>100 aAnd <250 Family Day >100 and 45
<250
45

1 - 100 90
B Seminar 85

>100 and
<250

◼ Write a C++ program to
a. prompt a user to input number of participants.
b. Calculate the total price

c. Calculate the total tax and total nett charge (subjected to 13%
25
government tax)

Exercise 6 Output example

26

Exercise 7

SHI Enterprise provides courier services. This company charges a shipping
charge based on the shipping destination as shown below:

State Code State Name Shipping charge
P Penang (M) per kg
8.00

A Perak 10.50

B Selangor 12.50

a. Prompt a user to key-in the state code.

b. Auto display the state name and shipping charge per kg based on
state code inserted.

c. Prompt a user to key-in the parcel weight.

d. Calculate the total charge.

e. Assume 5% service tax will be applied. Calculate the total amount
need to be paid after adding the service tax cost.


Click to View FlipBook Version