design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

5.2a

LO: To know how conditional statements react to events. (1)

We have seen how the while statement can evaluate conditions: if they are True then it will loop over a set of instructions, if they are False then the loop will not run. Reeborg has a list of conditions that it is able to evaluate to True or False:

We can think of these as things that Reeborg is aware of inside the different worlds.


Let's look at a simple example:


while front_is_clear():
    move()

			

Reeborg will evaluate the condition front_is_clear() to see if it is True - is the square in front of Reeborg empty? - If the condition evaluates to True then the commands inside the while loop will be carried out. The result of this that Reeborg moves forward until it crashes into a wall - ouch! What we need here is a set of commands that will run if the condition front_is_clear() is False. Let's make some changes to our original flow chart:

Now commands can be run if the condition is False, in code this would look like:


while True:
    #run commands if True in a loop
else:
    #run commands if False

			

We have now added an else statement (notice how it is indented to the same level as the while statement). We can now improve our program so that Reeborg does not crash into the wall:


while front_is_clear():
    move()
else:
    done()

			

Reeborg will check each time if the front square is clear, if it is then front_is_clear() will be True and the move() command will run. If the front square is not clear, then front_is_clear() will be False and the done() command will run.


Now we can look at the world Home1. Reeborg has to get to it's home and we are going to use conditional statements to help us:


while at_goal():
    done()
else:
    move()

			

at_goal() will check if Reeborg has reached his goal (home or finishing position), if this is True then Reeborg will be finished, if not Reeborg will move(). But! When we run the code Reeborg just takes a single step - what has gone wrong?
Remember that while is a loop and else is a way of breaking out of that loop if the condition is False - it is only run once. Our program is checking if at_goal() is True, once it finds it is False it runs the code in the else statement - but only once!
What we really need is a way of checking if Reeborg is not home. Here we can use 'not' in front of a condition to reverse it:


while not at_goal():
    move()
else:
    done()

			

Now Reeborg is checking to see if it is not home, if it is not then it will loop through the move() command until it is. If not at_goal() evaluates as True, then loop through the move() command until it is False, at that point run the code in else once (done()). Try the same code in the world Home1+ to see how it still works when the distance from home changes.

Code Examples

Resources