Snap Lesson 9: If’s
In this lesson we will learn about how to make decisions

So far all of our programs will do the something every time. Now we will learn how to have our code make choices.

If

• This block will only run the code inside of it, if the statement given to it is true
• If it is not true, or once it has run the code inside of it, it will run the code following it

So we can use this to create choices for our code. Lets start with a program that takes off, then turns right, and finally lands.

Then after the takeoff we will have it check a variable, if it equals 3 we will fly forward for 2 seconds.

Test this out. If you change x to 3 it should fly forward during the code, or it you leave it as a value other than a three it will not go forward during the code. However what if we wanted it to do one thing if x equaled 3 and another thing if it didn’t. For that we can use the if else block. This block will run the first part of the code if it is true, and if it is not true it will run the else part of the code:

This code will have the CoDrone fly forwards if x equals 3, otherwise if x does not equal 3 we will fly backwards.

Using if and if else statements we can make a lot of decisions. Think of some decisions you have made today. You probably thought sometime today that you were hungry. Inwardly your mind used and if statement, if I am hungry I will eat some food. So now we can make the CoDrone make decisions too.

Activity: Decide

The challenge is to have the CoDrone be able to land on two different landing pads, with only changing the value of a variable by 1.

Challenge Rules:

1. The course should be setup in a T shape. Start at the bottom of the T, and have the landing pads at the top ends of the T.
2. The landing pads should be the same distance to travel from the start.
3. You should travel up the middle of the T, then turn towards the select landing pad, and lastly fly to and land on the landing pad.