6 Logic
Learning Outcomes
- Define logic as the study of reasoning correctly and recognize its relevance in decision-making.
- Understand the purpose and usage of “if” statements in programming.
- Explain the role of comparators in comparing values within conditional statements.
- Demonstrate the implementation of “if-else” statements to create conditional logic in Blockly programming.
Logic
What is logic? You may have heard of someone being logical or reasonable (and maybe unreasonable and illogical!). Logic is the study of reasoning correctly. When we make decisions, do mathematics, or compare and contrast, we reason the best solution based on the scenario. Open the “Logic” menu to see the blocks available. What do you think they do?
If statement example
The first block in the list is an “if” block. You will be using this block to run different code depending on the condition. You actually use if statements every day!
- If my alarm goes off, then I will wake up. Else, I will stay asleep.
- If I am hungry, I will eat something.
- If it’s sunny out, I will wear sunscreen.
- If my room is clean, I will get my allowance. Else, I will make no money.
The if statement will check if a condition is True or False. The condition attaches to the if block. Let’s try an example. Grab the if statement block and drag it to the workspace.
Now, we are going to compare two values. Grab the comparator block.
From the math menu, choose the “number” block and changes the zeroes to any two numbers you want.
Finally, include a drive command for what Zumi should do if that condition is True.
Run the code! What happens? Now change the code so that the two numbers are the same.
Else statement
The first time you ran the code, nothing happened. This is because the original condition was False. In the example above, 3 is not equal to 4. That statement is not true, so the drive command will not run. This time, let’s add some code so the program can do something else if the condition was not true. Click on the blue gear and add an else statement.
The else statement will run if the previous condition is not true. Add a message for Zumi to display that the numbers are not equal.
Now when you run the code, if the condition is false, something else will happen instead. Try changing the numbers!
Comparators
In the past example, you were checking if two numbers are equal. The equal sign may look familiar from math class, but in code it has a slightly different application! The sign is an example of a comparator. Comparators can be used inside conditions to compare values. If the statement is true, the statement below it will execute. These are comparators in Blockly (they will look different in Python, but don’t worry about that yet!):
- Greater Than
- < Less Than
- ≥ Greater Than or Equal To
- ≤ Less Than or Equal To
- = Equal To
- ≠ Not Equal To
In the next activity you will combine variables and multiple if statements to compare two numbers.
Adding variables
In this next activity, you are going to compare two values X and Y. If X > Y, drive forward. If X < Y, drive in reverse. If neither of these are true, then X and Y must be equal! Write a message on Zumi’s screen, or do a little dance. First, create the variables X and Y and set them to any two values.
Multiple if statements
Begin with the first case, where X > Y. You will need an if statement, a comparator block, and a drive forward block.
Since there are more than two conditions possible, you are going to add in an else if block underneath the if block. Click on the blue gear and drag an else if block underneath the if block.
For the second case, if X < Y, Zumi should go in reverse.
If there were more cases, you can keep adding as many else if statements that you need. Since there are only three possibilities, if the first two are not True, then by default the program will run the else case, which means the numbers are equal! Click on the blue gear to add the else block. Then, add whatever code you want!
Change the values of the X and Y variables to make the program go through each condition.
Review
In this lesson, you learned how you can compare two values. In the future, you will be able to compare the values of the sensors to make decisions about which direction to drive in or when to stop driving. You will be able to use these if statements in loops to constantly make decisions based on the environment.
Test Your Knowledge
Review The Key Concepts
Solve The Problem
Scenario: You need to define two variables, x and y, and compare their values. Based on the comparison, Zumi will react differently:
- If x is equal to y, Zumi will perform a celebration action.
- If x is greater than y, Zumi will move forward for two seconds.
- If y is greater than x, Zumi will move backward for two seconds.
Your task is to create a Blockly program that implements this logic, allowing Zumi to respond correctly to the different conditions set by the variables x and y.
Problem: Develop a logical code structure using Blockly to control a robot named Zumi’s movements based on the comparison of two variables, x and y.
Sample Solution
There are many different ways that we can solve the problem listed above. Please watch the following video to review a sample solution completed by Ontario Tech University Engineering Students.
Demo
Code
Common Errors
Errors can also occur while creating a solution to the problem. Please review the video below to view some of the common errors that can occur.