Unit B · Telling a computer what to do
What we are making: a program that behaves differently depending on what is happening — the first time your program makes a decision instead of just obeying.
Everything so far runs the same way every time. Real programs do not. They look at something, and then choose.
The block is called if. It asks a question that can only be answered yes or no, and it runs what is inside it only when the answer is yes.
If the cat is not touching the edge, the say block is skipped entirely, as if it were not there. Then the program carries on.
The if block needs to be checked over and over, so it lives inside a forever loop. That pairing — forever plus if — is the shape of almost every program you will write this year, including the robot ones in Unit C.
If / else has two arms. One of them always runs.
| What you see | Why | What to do |
|---|---|---|
| The if never runs | It is not inside a forever loop. | A check has to happen over and over to be useful. |
| It runs constantly, not once | That is correct behaviour for a forever loop. | If you only want it once, use 'wait until' instead of 'if'. |
| Nothing in the else arm ever happens | The condition is always true. | Test the condition on its own first — click the block to see true or false. |
| It flickers between the two | The condition is right on the boundary. | You will meet this properly in Level 2. For now, move the boundary. |
In this session