Lesson 3: Conditional Statements
A conditional statement is a set of rules performed only if a certain condition is met. Sometimes we call it
If-Else or If-Then statement. For example, if tomorrow is Saturday, Marry do
not need to go to school.
In our world, the robot can understand conditions too! We went though some "if this, then that"
from previous lesson when we were talking about sensors. Like,
If the ultrasound sensor sees anything in 1 meter, the robot would stop for 2 seconds.
In this example, the action "robot would stop for 2 seconds" will only be executed when the condition
"ultrasound sensor sees anything in 1 meter" is met.
The conditional statement can be complex if you want. For example:
If condition A, then the robot moves forward.
Else if condition B, then the robot moves backward.
Else if condition C and condition D, then the robot turns left.
Else, end the program.
Don't forget to add the comma between the condition and action! That helps the system to parse the sentence!
One important thing to check is if the logic is conflicting with each other, if the statement is an infinite
loop, if there is "bug". For example,
If the touch sensor touches anything, the robot becomes motivated.
Otherwise, the robot is happy.
If the robot is happy, the robot goes forward.
Else if the robot is motivated, the robot turns left.
Can you tell me why this program is buggy?
Now, let's take a look at the other version of the same program.
If the touch sensor touches anything, the robot becomes motivated and the robot is not happy.
Otherwise, the robot is happy and the robot is not motivated.
If the robot is happy, the robot goes forward.
Else if the robot is motivated, the robot turns left.
Can you tell me the difference?
👈 Previous: Lesson 2
🏠 Homepage
📚 Lesson plan
👉 Next: Lesson 4