Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.4, Edexcel 1CP2 6.5.2 · about 12 min
Relational operators, and a block that runs only when a condition is true.
[1 mark]What does this program print?
speed = 40
if speed > 50:
print("fast")
if speed > 30:
print("medium")
if speed > 10:
print("slow")
print("checked")medium slow checked
Three separate ifs are each checked. 40 > 50 is False; 40 > 30 and 40 > 10 are both True.
[1 mark]What is the difference between = and ==?
[1 mark]Which comparison means "is not equal to"?
!=<>=!not=[1 mark]What does this program print?
d = 25 print(d < 30) print(d >= 30)
True False
A comparison gives True or False.
[1 mark]What happens to the block under an if when the question is False?
[1 mark]What does this program print?
x = 7
if x % 2 == 0:
print("even")
print("done")done
7 % 2 is 1, so the question is False and the block is skipped.
[1 mark]What does this program print?
answer = "Yes"
if answer == "yes":
print("driving")
print("done")done
"Yes" with a capital Y is a different string from "yes", so the condition is False.
[1 mark]d is exactly 40. Which condition is True?
d <= 40d < 40d > 40d != 40[1 mark]How does AQA pseudo-code write "is equal to"?
======EQUALSDrive 20 cm. Then, with two separate ifs: if the wall is less than 40 cm away, print near and make the LED red; if it is 40 or more, print far and make it green. The wall has been placed so the answer should be near, but write both.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
forward(50, distance=20)
d = distance()
if d < 40:
print("near")The hint students can ask for: Drive 20 cm. Then use two ifs: if the wall is under 40 cm away print 'near' and make the LED red; if it is 40 or more, print 'far' and make it green.
from bugbot import *
connect()
forward(50, distance=20)
d = distance()
if d < 40:
print('near')
led('red')
if d >= 40:
print('far')
led('green')
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.