
Variables: More examples

Upgrade for more content
True or false? There are different sorts of variables.
Look, I have reprogrammed the robot so it can ask us both what we are called. And how old we are. Good isn't it? Let me see! Hello Maria age 15.
Hello Lina age 16. Wow! The robot knows how old we are. Can the robot say which of us is older than the other? Hmm, then we need to code a little first.
Maria and Lina want to teach the robot to say which of them is older than the other. Let's start by looking at the pseudocode. Here we input the first person's name, and save it as the variable name 1. Then input the first person's age as the variable age 1. Same thing for the second person.
That makes four variables altogether. Let's look inside the robot's brain, and take a look at these variables. Here's a bureau.. What's inside the drawers? Here we have the names: Maria, and Lina.
And there the ages are stored. What could be stored in the drawer with label "age 1"? 15! Maria's name and Maria's age are stored in different kinds of drawer. They are different kinds of variable.
Name is text. Age is integer. That's pretty smart, because it take less space to write an age than to write a whole name. So the robot can use smaller drawers for the numbers. Also, integer and text are rather different.
The robot can do different things with them. It can for example, compare Maria's and Lina's ages, with the help of a subtraction. It can't do that with two names. So, when we use variables in programming, we have the advantage of sorting them into different kinds, with different characteristics. Here's another drawer: Decimal number!
And here's a special kind: a very tiny drawer, which contains only true, or false. So now, let's teach the robot to compare Maria's and Lina's ages. In the pseudocode we write "Note" and then "age 1" to tell the robot to put something in the drawer with the label "age 1". When we assign a value to a variable, we use the equals sign. Wait, the equals sign means that age 1 equals 15?
Well, in mathematics, it would mean that: the left side is equal to the right side. But now, on the other hand, we are programming, and here the equals sign means that we're adding - assigning - the variable with the name 'age 1' the value 15. We're adding 15 into its drawer. Now we want the robot to calculate the difference between Maria's and Lina's ages. Then we use the minus sign to get the difference.
We add this line to our pseudocode. The arithmetical subtraction with the minus sign works just as in mathematics. Addition, multiplication, and division also work the way we are used to. The last row of the pseudocode outputs the result. A flowchart of our pseudocode makes it clearer.
Put the first name in n1. It's good to use short variable names so we don't have to write too much in our code. Put the first age in age1. Put the second name in n2. Put the second age in age2.
Calculate the difference and call it 'result'. Output the result. Shall we see if it works, then? - Name? - Maria - Age? - 15 Now it is your turn Lina. - Name? - Lina - Age? - 16 Maria is minus 1 year older than Lina. Ha ha I am older than you! Yes, minus 1 year older hahaha!