0% found this document useful (0 votes)
40 views15 pages

T2 Sequence and Selection

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

T2 Sequence and Selection

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

Objectives

• Use selection and nested selection statements


• Use NOT, AND and OR when creating Boolean
expressions
• Use random number generation
Sequence and selection
Unit 7 Programming

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

• Comparison operators include


== equal to
!= not equal to
> greater than
< less than
• What are two other comparison operators?
• Why is = not used to mean equal to?
Sequence and selection
Unit 7 Programming

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

Complex Boolean expressions


• Boolean expressions can include the Boolean
operators AND, OR and NOT
• For example:
if (mark < 0) OR (mark > 100) then …
Operator Description

AND Returns TRUE if both conditions are TRUE

OR Returns TRUE if either of the conditions are TRUE

NOT A TRUE expression becomes FALSE and vice versa


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)

82 80 (mark1 >= 80) OR (mark2 >= 80)

35 (mark1 > 30) OR (mark1 < 50)

65 (mark1 < 30) OR (mark1 > 80)

0 75 NOT(mark1 > 50) AND (mark2 > 50)

65 85 NOT(mark1 < 60) AND NOT (mark2 < 80)


Sequence and selection
Unit 7 Programming

Worksheet 2
• Now complete Task 1 on Worksheet 2
Sequence and selection
Unit 7 Programming

The switch/case statement


• This statement may be used when a selection is to
be made from several alternatives, for example
when choosing from a menu
switch menuChoice:
case "1":
print("You selected 1")
case "2":
print("You selected 2")
case "3":
print("You selected 3")
default:
print("This is not a valid choice")
endswitch
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

You might also like