Sequence&Selection
Sequence&Selection
Python and other procedural language make use of three basic programming
constructs ( Sequence , selection and repetition).
Combining these constructs provides the programmer with the tools required to
solve logical problems
Sequence
Sequence is indicated by the order in which the code is written, usually top
to bottom.
Sequential execution is when statements are executed one after another in
order.
Statements are followed in sequence so the order of the statements in a
program is important.
Selection
Selection used to determine a different set of steps to execute based on a
decision (condition).
Code branches and follows a different sequence based on decision made by
program.
Selection in Python Selection in pseudo
if statement IF..THEN..ELSE..ENDIF
if..elif..else
IF statement in Python
A statement that allow the program to follow or ignore a sequence of code
depending on the decision (condition).
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.
Else is optional
Python is its use of indentation to highlighting the blocks of code.
Syntax Code :
if condition/decision : num= int (input(“Enter Number”))
code if num > 0 :
code print(“positive”)
else : else :
code print (“negative”)
Example
Design a system that will takes as input two whole numbers. If the second
number is larger than the first, the system will output ‘second’; if not the output
should be ‘First’.
Nested IF statement
Nested if statements are an if statement inside another if statement.
Example
The previous example is expanded to produce a third output “same” when
the numbers are the same
if-elif-else statement
Syntax Code:
Example
It is measure of body mass index ( BMI) based on height and weight of individual
(BMI = weight / (height * height) ). Using the range of BMI , individual is
classified as underweight, normal, overweight or obese. The following table shows
the main BMI categories
Category BMI Range
Underweight less than 18.5
Normal Between 18.5 to 24. 9
(inclusive) 18.5 <=
Bmi <= 24.9
Overweight Between 25 to 29.9
(inclusive)
Obese Greater than and equal
to 30
Grade Condition
A Student achieved 80 or more of the mark
B Student achieved 70 or more of the mark
C Student achieved 60 or more of the mark
U Student achieved less than 60 of mark
2. A system is required to calculate the delivery cost of parcels. All parcels have a
fixed charge if the parcel is 5 kg or below in weight. For local delivers this charge
is $ 20; for international delivers the charge raises to $ 40.
The maximum weight limit for international deliveries is 5 kg; however, for local
deliveries extra weight is permitted and charged at $ 1 for every 1 kg above that
limit.
3. Write a program that input three number and output the multiply of largest and
second largest number.
4. House owner must pay tax based on the value of the house. Houses over $
200000 pay 2% of their value in tax, houses over $ 100000 pay 1.5% of their value
in tax and houses over $ 50000 pay 1% of their value in tax. All others pay no tax.
Write flowchart which :
Input the value of house
Output the tax of house.