0% found this document useful (0 votes)
3 views

Sequence&Selection

The document explains the three basic programming constructs in Python: sequence, selection, and repetition. It details how sequence dictates the order of code execution, selection allows branching based on conditions, and repetition enables code to run multiple times. Additionally, it provides examples of if statements, nested if statements, and exercises related to programming logic and decision-making.

Uploaded by

winhtikeaung43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Sequence&Selection

The document explains the three basic programming constructs in Python: sequence, selection, and repetition. It details how sequence dictates the order of code execution, selection allows branching based on conditions, and repetition enables code to run multiple times. Additionally, it provides examples of if statements, nested if statements, and exercises related to programming logic and decision-making.

Uploaded by

winhtikeaung43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Sequence, Selection and Repetition

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.

num = int (input ( “ Enter a number”))


res= num * 2
print (res)

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”)

To support the needs of these type of decisions/condition, comparison operators


/relational operators exist.

Description Pseudocode Python


is equal to = == (= assignment )
Is greater than > >
Is less than < <
Is greater than or equal >= >=
Is less than or equal <= <=
Is not equal to <> !=

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

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


statements.
Instead of a series of nested if statements , python would use elif.

Syntax Code:

if condition : first= int(input(“Enter first number”)


Code second= int(input(“Enter first number”)
elif condition:
Code if first = second :
.
print (“same”)
.
else : elif first > second
Code
print (‘First’)
else :
print(‘second’)
Connection comparison operator or condition
Often a single condition or comparison operator is not sufficient to define the
required criteria.

Logical Description Example in Python


Operator
And Both If mark >-1 and mark < 101 : range chcek
Or Either or If smoke = Ture or temperature > 70 :
Not If not num == 6 :

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

Write a program to determine whether a person is the situation of underweight,


normal, overweight or obese which:
 Input the weight (kg) and height(m) of person
 Output underweight or normal and etc based on BMI
Exercise
1. A program required to take as input values the number of marks a student
achieved available in an examination. It should the output the grade obtained based
on the grade boundaries in table.

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.

5. Construct the flowchart which


Takes three numbers as the input
Output the largest number

You might also like