seegongsik
Programming

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.

01

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.

Number
25
you can calculate
Text
"Minjun"
words or a name
True·false
true
yes / no
Numbers have no quotes, text is wrapped in quotes. True·false is one of two.

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.

02

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.

Number3 + 47Add numbers and it's calculation (3 plus 4 is 7)
Text"3" + "4""34"Add text and it sticks together (4 next to 3)

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.

03

"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.

3 + 4
number + number
result
7
It's a number, so it calculates. 3 plus 4 is 7.

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.

04

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.

Is 10 bigger than 7?
The answer is true, because 10 is bigger than 7
True·false is just one of two. To a computer, true is 1 and false is 0.

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.

05

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.

Number box
Still empty

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.

In one lineValues come in types (numbers, text, and true or false), and the type decides what you can do with a value. "3" and 3 look alike but are different values, so a single pair of quotes decides between calculating and joining.
← Programming
Was this helpful? Support seegongsik