Sequence, selection and iteration Flashcards
What are the three basic control structures used in programming?
Sequence, selection and iteration
What are the three operators used in Boolean expressions?
AND, OR, NOT
What is Sequence?
The statements are executed one by one in the order they are written
e.g.
mark1 = 78
mark2 = 67
total = mark1 + mark2
average = total / 2
print(average)
What is 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
e.g.
if average >80 then
print(“Distinction”)
else
print(“Pass)
endif
Give an example of a Boolean expression
average >= 80
The outcome will always evaluate to TRUE or False
What are the comparison operators?
== equal to
!= -not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Why is = not used to mean equal to?
A single = is used for assignment - e.g. age = 14
What is Iteration?
a process wherein a set of instructions or structures are repeated in a sequence a specified number of times or until a condition is met
e.g.
repeat
<statements>
until <condition></condition></statements>
What are three types of programming loops?
FOR loop, WHILE loop, DO WHILE loop
When to use for loops
When you want to execute the loop a specified number of times
e.g.
total = 0
for i = 1 to 7
dailyTempurature = input()
total = total + dailyTempurature
next i
averageWeeksTemp = total / 7
print(averageWeeksTemp)
When to use while loops
When you want to execute the loop while a certain condition is true
e.g.
password = input(“Please enter password: “)
while password != “jd8UpP2+d”
password = input(“Invalid password - try again”)
endwhile
print(“Correct password”)
What is an infinite loop?
If a loop continues forever and there is no way to way to exit it, it is called an infinite loop
When to use do … until loops
When you want to execute the loop until a certain condition is True
The loop will always execute once
The condition is tested at the end of the loop
e.g.
password = “ “
do
password = input(“Invalid password - try again”)
until password == “jd8UpP2+d”
print(“Correct password”)
What loop should you use for this situation:
Ask a user to enter their name - if they don’t enter a name longer than one letter, then keep on asking them to enter their name
DO WHILE loop
What loop should you use for this situation:
Count from 1 to 100
For loop