T2 Sequence and Selection
T2 Sequence and Selection
Starter
• What are the three basic control structures used
in programming?
• What are three operators used in Boolean expressions?
Sequence and selection
Unit 7 Programming
Sequence
• The statements are executed
one by one in the order they
are written:
mark1 = 78
mark2 = 67
total = mark1 + mark2
average = total / 2
print(average)
Sequence and selection
Unit 7 Programming
Comparison expressions
• The condition average >= 80 is a
Boolean expression
• The outcome will always evaluate to TRUE or FALSE
Selection
• An IF statement is a selection statement
• The next statement to be executed depends on
whether the condition being tested is True or False
if average >= 80 then
print("Distinction")
else
print("Pass")
endif
Sequence and selection
Unit 7 Programming
If statements
• If statements allow different branches to be executed
based on the result of a Boolean expression
if average >= 80 then
print("Distinction")
elseif average >= 60 then
print("Merit")
elseif average >= 40
print("Pass")
else
print("Fail")
endif
Sequence and selection
Unit 7 Programming
Nested if statements
• If statements may be nested:
if member == "child" then
if day == "Saturday" then
swimPrice = 2.00
else
swimPrice = 2.50
endif
else
swimPrice = 4.00
endif
• What is the price for an adult on Saturday?
• What is the price for a child on Sunday?
Sequence and selection
Unit 7 Programming
True or False?
• Complete the table:
True or
Mark1 Mark2 Condition
False?
80 67 (mark1 >= 80) AND (mark2 >= 80)
Worksheet 2
• Now complete Task 1 on Worksheet 2
Sequence and selection
Unit 7 Programming
Random numbers
• Programming languages will provide a number of
built-in functions that can be used
• To use them a library may need to be imported
• For example, in Python, import random will import the
random library of functions
• A random number between 0 and 100 can then be
generated with a statement such as
random(0, 100)
In Python this would be random.randint(0, 100)
• How could you simulate the throw of a die?
Sequence and selection
Unit 7 Programming
Worksheet 2
• Now complete Task 2 and Task 3 on Worksheet 2
Sequence and selection
Unit 7 Programming
Plenary
• Look at the following code:
hourlyRate = 15.50
hours = int(input("Enter hours worked this week: "))
if hours > 168 then
print("That’s impossible")
else
totalPay = hours * hourlyRate
print(totalPay)
endif
• With a partner, identify each of the following:
• A comparison operator, a Boolean expression
a selection structure, a sequence, three assignment operators,
an output statement, one mathematical operator