1

Conditionals

Conditionals are statements that help programs make decisions depending on if the statement is true or false. In Blockly, conditionals include if, else if, and else statements. You actually use conditionals to make decisions in your own life! This is how you might use if statements, which usually look like “if _____, then _____.”: 

  • If it is my birthday, then I will eat birthday cake. 
  • If it is sunny, I will play outside. 

If you want to include an action that will be performed if the if statement is false, you can use an else statement. This is how you would an else statement, which looks like, “if _____, then ______. Else, _____” with the statements above. 

  • If it is my birthday, then I will eat birthday cake. Else, I will not eat birthday cake. 
  • If it is sunny, I will play outside. Else, I will stay inside.

If you want to be able to make another decision between the if and else statements, you can use an else if statement. This is how you might use if statements, which usually look like “if _____, then _____. Else if _____, then _____. Else, _____.”: 

  • If it is my birthday, then I will eat birthday cake. Else if it is my friend’s birthday, I will eat birthday cake. Else, I will not eat birthday cake. 
  • If it is sunny, I will play outside. Else if it is cloudy, I will wear a jacket and play outside. Else, I will stay inside. 

Conditionals are used the same way in your Blockly programs. This is one example: 

  • If the up button is pressed, then the CoDrone will fly up. Else if the down button is pressed, the CoDrone will fly down. Else, the CoDrone will hover in place.

The if statement block is in the light purple logic menu and has two parts.The first part is the notch next to the if, and this is where you would put the condition. This is the “if _____” part of an if statement. What you put in this space can only be true or false. Some examples include: 

  • If the up button is pressed 
  • If 2 is even 
  • If the battery percentage is less than 25 

The next part is the space beneath the “if _____” part of the if statement. This is where you include the “then _____” part of the statement. You can do this by finding the block you need in the menus, and then dragging and dropping it into that space so the blocks connect. 

This is how an if statement works once you run it: 

  • Your program will check the if statement.
    • If the condition is true, it will run the code within the if statement, and once that’s finished, your program will run the code after the if statement.
    • If the condition is false, your program will skip the if statement and run the code after the if statement.

You have everything you need to program your first if statement! Program your drone to take off and then throttle up if 5 is greater than 10. You can find an equation box in the light purple logic menu, and you can select inequality signs from the drop-down menu. Number blocks are in the gray math menu. Don’t forget to have it land! Next, use the same program but switch the greater than sign to a less than sign…just to see what happens! There might be times when you want your program to do one thing if the condition is true and a completely different thing if the condition is false. In those cases, you would use an else statement, which is part of the if statement block. Just click on the blue gear icon and then drag and connect the else block to the if statement block. Else statements are usually written as “If _____, then ______. Else, _____.” The first part of the else statement block looks just like an if statement! There’s a space for your condition and another space for what happens when your condition is true. The second part of the else statement is for the “Else, _____” part of your else statement. To include what action should be taken if the program is false, find the block you need in the menus, and then drag and drop it into the space underneath the else so the blocks connect. 

This is how an else statement works once you run it: 

  • Your program will check the if part of the statement.
    • If the condition is true, it will run the code within the if statement, and once that’s finished, your program will run the code after the else statement.
    • If the condition is false, your program will run the code within the else statement, and once that’s finished, your program will run the code after the else statement.

Program your drone to take off and then so that if pi, which is in the gray math menu under its symbol, is greater than 3, it will spin in place. Else, it will fly forward and then to the left. Sometimes you might want to have more than two options when making a decision. If that is the case, you can use an else if statement between your if and else statements. To use an else if statement, click on the blue gear icon in the if statement block from the light purple logic menu, and then drag the else if block to the if block. You’ll see that just like an if statement block, there is a notch for your conditional and then a space for what will happen if the condition is true. This is how an else statement works once you run it: 

  • Your program will check the if part of the statement.
    • If the condition is true, it will run the code within the if statement, and once that’s finished, your program will run the code after the if statement.
    • If the condition is false, your program will move on to the else if statement. If the condition is true, it will run the code within the else if statement, and once that’s finished, your program will run the code after the else if statement.
    • If the condition is false, your program will run the code within the else statement, and once that’s finished, your program will run the code after the else statement.

You can have as many else if statements as you want in a program! Just remember there should always be a beginning if statement. Program your drone so that if 7 is an even number, your CoDrone will turn left for 1 second. Else if 8 is an even number, your CoDrone will fly backward for 1 second. Else, your CoDrone will hover for 2 seconds. The even and odd blocks are in the gray math menu.After testing this version out, use the drop-down menus to change your options. What happens when you set both numbers to odd, or when 7 is even and 8 is odd? Challenge: CoDrone Calculator 

Get your CoDrone to do your math for you! Program your CoDrone to fly in different patterns based on if the mathematical equations you answer are true or false. 

Rules: 

  • You must use at least one if statement and one else statement. 
  • Don’t forget an emergency stop! 

Hint: 

There are blocks that are more complex than the number block. If you want to use these in an equation, drag the light purple equation block to your workspace and then drag the block you need into one of those blank spaces.