
If (programming)

Upgrade for more content
True or false? The instructions for the robot's program is called pseudocode.
I’ve been given a box of chocolates. Do you want one? Yeah! Only milk though. I only want dark.
Here is a dark chocolate. Milk chocolate! You’ll like this, Maria. Another... Here.
Me too… More! Hey! You can take them yourselves. I am not going to bring chocolates to you… But my robot might be able to do that! Oh but the robot doesn’t know what we like.
It doesn’t know which chocolate we’d choose. It can be taught. Lina will teach the robot to give chocolate to the right person. Maria likes milk chocolate. It is her requirement if she is going to eat it.
Leon’s requirement for eating chocolate is that it is dark. Requirements control what the robot should do, they control the conditions. A condition can just be true or false, fulfilled or not fulfilled, never something in between. Now, Lina will explain to the robot what it should do. The robot must have everything clearly explained.
The instructions are called the pseudocode of Lina’s program. The pseudocode looks like this: Pick up a chocolate. If it is milk chocolate give the chocolate to Maria. If it is dark chocolate give the chocolate to Leon. Lina indents these two rows in the pseudocode.
Why? To make it clearer how the conditions control the robot. The robot reads this “if”-instruction. If the condition is true the next instruction is performed. If the condition is false then the robot jumps over all the instructions that are connected to that “if”-row.
The instruction “Give Maria the chocolate” Is only performed if the condition "milk chocolate" is true. The instruction “Give Leon the chocolate” Is only performed if the condition "dark chocolate" is true. When Lina indents the two rows of the pseudocode, it is clearer how the conditions "milk" or "dark" chocolate control which instructions are to be performed. Now let's see if the robot learned anything. The robot picks up a chocolate.
It is a dark chocolate and the first condition is ...false. The robot then jumps over the instruction to give Maria the chocolate. Then comes the next row. The condition is dark chocolate. This condition is true.
The robot has a dark chocolate in its hand. So the robot performs the instruction on the next row, giving the chocolate to Leon. Maria did not get any chocolate so she restarts the robot's program. The robot picks up a new chocolate. This time it is a milk chocolate and the first condition is ...
true. Therefore the robot gives Maria the chocolate. The program continues to the next row and the robot stands there with an empty hand. It has no chocolate at all! Is the condition “Dark chocolate” then true or false?
We said that programming conditions can only be either true or false, and nothing in between. No chocolate means that the condition "Dark Chocolate" is false, because there is no chocolate! So the program skips the instruction to give Leon chocolate and stops. By using conditions, the robot can give Maria and Leon the chocolate they like best. Everyone is satisfied and happy.
Good robot!