Sometimes the tasks you wish your robots to perform can have different solutions depending on weather, environment and other conditions.
- 04 Jun 2020
Have you ever forgotten where you've left your phone?
If you've forgotten where you've put your phone, you would start checking where you have been and if the phone is there. During all of this, you are constantly doing the following checks:
- If my phone is here, I stop searching.
- If my phone isn't here, I keep searching.
Such tasks require us to program the robot to perform checks and make decisions based on them.
The "if" logical operator allows us to program our robot to perform its actions based on rules set by you.
For it to know what it's doing, the "if" operator requires a condition or statement that can be "True"/met or "False"/unmet to be given.
Here's how this command should look in your code:
if condition: (conditionally executed code) (code that will always be executed)
⚠️ Notice the colon right after the condition. It's common to forget to put in this colon in the beginning and for the students to wonder why the program doesn't work!
If the condition is met or "True", the program will execute the lines of code that are indented inward. If the condition is not met or "False", those lines will be skipped in the execution. There is no difference between indenting with several spaces or a TAB, so long as it is the same throughout the whole program.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons