Lesson 4 IF and IF ELSE

IF ELSE and the Walk Around Simple Scratch Project

What are the IF and IF ELSE blocks?

You will have learned the most basic part of coding when you have completed this lesson. The reason we say this is because the most basic needs of a program are:-

  • 1: Putting sequences together.
  • 2: Working with variables.
  • 3: Loop though code.
  • 4: Make decisions in IF statements.

There is lots more but you are starting to get to know Scratch by now. You should be able to think of a small program you can make for yourself. This is a big step along the way a proficient coder. So very well done.

An IF test will always have a result of true or false. Like 1 = 1 will be true and 1 = 2 will be false. This will sound very strange, because 1 = 1 of course it does - but we are just making a point. In fact, when we are testing code we will use 1 = 1 when we need to guarantee a true response and similarly 1 = 2 when we need a false response. We will have a lesson on how to get your code working well later. We call finding mistakes in your code 'debugging'.

The demo.

Download IF Explained Project


We have 3 demos here, which will be getting a bit more useful as we go though them.

A:-

The first demo is a IF statement on its own (we have highlighted it in red with the test). The program is waiting for you to type something.

The test is for the word 'left'. The left hand will only raise if you type left.
Any thing else is not the word 'left'.
So if our answer = 'left' the test is true and the code inside the bay will be made to happen. We call that 'executed'.
Can you see how, by looking at the code, the left hand will raise and then go down?
The answer could = something else like 'right', the answer is not = to 'left'. The test is 'false'.
None of the code in the bay is executed and the code move on. In this case it will just finish.

B:-

We will now do a test for is 'left' but this time it is in a IF ELSE test. It means we can do something else if the test is 'false'. At first glance you may think it will raise the left arm if word = 'left'. and then rise the right arm if you enter the word 'right'. This is correct, but with a maybe unexpected outcome, because if we enter anything else like 'both'. It will raise the right arm. Test it for yourself.

C:-

In the last demo you can see that we have moved the code to put the hands down after the IF tests. Our code has four IF tests, one after another. We set a WAIT UNTIL block to look for the left, right, up and down keys. We have cut this a little bit out of the the picture to make it fit, but look at the project. Each block is tested one after another and the code is used. We can use one IF test after another, to test what key press is true. As long as we know all of the possible key presses, the user can use at the time you write your program.  This technique in coding is dependent on you to think of every outcome.

One More IF Demo

Download IF Explained Project





This demo asks a question. As we saw before the answer is stored in the light blue answer variable. When the user click the tick, an IF block checks to see if the answer is 'left' and moves the cat to the left of the screen if true. If it is any thing else than 'left' it will do nothing. The next IF block has an ELSE bay with it. So if the user types 'right' it will move the cat to the right and the code moves on, but any thing else it will move the cat up.

So:

Type

  • 'left' the cat moves left then up.
  • 'right' the cat moves right only.
  • 'up' the cat moves up.
  • 'down' the cat moves up. The cat will also move up if you type an other word.

Look at the code can you see this logic in the code? Point you finger at the top block and move down on block at a time write you answer in this box.

Then work down the code block by block answering the IF question one by one. You will understand if you keep at it.

The Simple Walkaround project

Download the Simple Walk Around Project

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.




The walk around project is now going the have our cool kid walking around the full square. We have written the code for this, as it is a bit complicated. What we would like you to do is put the code together, so the cool kid can glide around the path. You have three blocks to put together in a sequence to get the cool kid in the starting position. All the rest of the code is now going to go in the FOREVER block. As we did in the last lesson we put the glide code in the REPEAT UNTIL red is touching green (The head of the cool kid is red, and the edge of the path is green. So if the end of the path is not reached the red head is touching grey not green.). Which is kept simple by using the SpeedX and SpeedY variables. After the loop exits we have 3 IF ELSE blocks. The first one will test if we are going up and down, or side to side. We use the next 2 IF ELSE blocks in a nested IF block that is putting it inside the outer IF block. (We are covering nesting in the next lesson!). The IF ELSE blocks will test if we are going up, down or left, right. Then the variables are set to set the speed and jump to be on the path.

I Need more help.


This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.





This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.