Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Introduction to

Programming
Computer Science
Lesson 3
Unit 8.2
Objectives

• Understand what Sequence of code means

• Understand what Selection in Programming means

• Use Selection and Sequence


What is a Sequence?
Sequencing is the specific order in which instructions are performed. This basically means

code is followed line by line in order!


Why is Sequencing
important?
Sequencing Example 1
1. Using a Pencil, Paper 2. Using a Pencil, Paper
and Ruler follow these and Ruler follow these
instructions. What do instructions. What do
you get? you get?
Sequencing Example 2
When this program has finished
running, what will be the values of A
and B? What has happened?

A 5
A = 10
B=5
B
A=A+B 10

B=A-B
A=A-B
Exercise 1
Open Worksheet 1 on Firefly and complete Task 1
What is a Selection?
We need to make decisions all the time – what time to get up, what to eat,
how to start your essay, etc.…

In programming, the word Selection has the same sort of meaning.


Can you think of a program that does exactly the same thing every time?
Most programs make some sort of decision
Use of Selection
What sort of things will your program make selections based on?
Your program might need to react to:
- what the user does
- what happens in the program
- the values of particular variables
- an external factor, such as date or time
Show Video
If Statements are Selection
If statements are a way of using selection in programming. They allow the
program to make a decision.

What selection is the Flow Diagram making?


If Statement Structure
if age >= 70:

Selection 1 print("You are aged to perfection!")

Else:

Selection 2 print("You are a spring chicken!")


If Statement Examples 1
What would be the output of this program?
a = 10
If a >35:
Print (“Good score!”)
Else:
Print (“Poor score!”)
What if the value of a changed to:
a = 100 a = 35
a=0 a = 36
Nested If Statements
Nested if Statements are if statements with several outcomes, not just two! These
allow the program to have several options, for instance:

Which output would you be?


Nested If Statement Structure
if age >= 70:
Selection 1 print("You are aged to perfection!")
elseif age =50:
Selection 2 print("Wow, you are half a century old!")
Else:
Selection 3 print("You are a spring chicken!")

Which output would you be?


Reminder of Operators
Operators also allow
you to compare data
in programming,
which will also let
your program make
decisions.
Exercise 2

Open Worksheet 1 on Firefly and complete Task 2


What did we learn today?
What is sequencing and using it

Understand the structure of if statements and write them

Understand nested if statements and write them

What are operators and how to use them

You might also like