What are data types
We've stored values in boxes and shown them on screen. But is a value a number or text? Values come in types, and what you can do with a value depends on its type.
Values come in three main types
The values that go into variables aren't just anything; they have types.
Numbers you can calculate with, words made of text, and values that say true or false.
These three types are the basics of the values a program handles.
The three types of values.
A number is just 25, text is wrapped in quotes like "Minjun", and true or false is one of two.
You can tell the types apart by how they look.
The computer looks at the type too, and treats each value differently.
Even adding depends on the type
The same "plus" means different things depending on the type.
Add numbers and you get a calculation; add text and the pieces get joined together.
In other words, the type decides what happens.
The same +, different results depending on the type.
3 plus 4 is 7, but "3" plus "4" becomes "34".
Seen as numbers, it's a calculation; seen as text, it's joining.
The very same + gives a different result because of the type.
"3" and 3 are different
To the eye they're the same 3, but with quotes it's text and without quotes it's a number.
You can't calculate with the text "3"; only the number 3 can be used in a calculation.
Toggle the quotes below and see how the result of 3 plus 4 changes.
One pair of quotes changes the type.
One pair of quotes decides whether you get a calculation or a join.
So if you mix up types, the program does something odd.
"3" and 3 look alike but are different values.
True and false are the third type
The third type is true and false.
It's a value with only two possibilities: "yes" and "no."
When the ALU in the structure section answered true to "Is 10 greater than 7?", that was this type.
A value that's one of two.
True and false are simple, being just one of two, but they're the most important type.
That's because the "if" we'll see in the next lesson uses exactly this true or false to choose a path.
The type has to match to fit
You can set a type for a box (variable) too.
Say "this box takes numbers only," and when you try to put text in, it shows that it doesn't fit.
Enter a value and switch the box type to check whether they match.
Check whether the box type and the value match.
Setting types prevents mistakes caused by the wrong kind of value getting in.
Numbers in the number box, text in the text box.
Every value has a type, and sticking to those types is a basic part of programming.
In the next lesson, we'll use true and false to build forks in the road.