seegongsik
Saved words
Programming

Numbers, text, true and false

We put values in boxes and showed them on screen. But is that value a number, or text? Values have kinds too. What you can do with one depends on its kind.

01

Values come in three main kinds

A value in a variable
isn't just anything;
it has a kind.
A number you can calculate with,
text made of letters,
and true or false.
These three kinds
are the basics of 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 kinds of value.

A number is just 25,
text is "Minjun" wrapped in quotes,
true·false is one of two.
The shape itself
tells the kind.
The computer too looks at the kind
and treats the value differently.

02

Even adding differs by kind

The same "add"
means something different
by kind.
Add numbers
and it's calculation,
but add text
and it sticks together.
The kind decides
what gets done.

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 + gives different results by kind.

3 plus 4 is 7,
but add "4" to "3"
and you get "34".
As numbers it's calculation,
as text it's sticking together.
The same +,
but the kind changes the result.

03

"3" and 3 are different

To the eye it's the same 3,
but with quotes it's text,
without them it's a number.
Text "3"
can't be calculated.
Only the number 3 can.
Toggle the quotes on and off
and watch how 3 plus 4
changes.

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

One quote mark changes the kind.

One quote-mark's difference
makes it calculate
or stick together.
So if you confuse the kind,
the program
behaves strangely.
"3" and 3
look the same but are different values.

04

True and false are the third kind

The third kind
is true and false.
"Yes" and "no,"
a value with just two options.
Last time the ALU
answered "is 10 bigger than 7"
with true,
that's exactly this kind.

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·false
has only two options,
so it's simple,
but it's the most important kind.
The "if this is so"
we'll see next time
chooses a path
with exactly this true·false.

05

The kind has to fit to go in

A box (variable) too
can have a kind set.
Say "only numbers in this box,"
and when you try to put text,
it tells you it doesn't fit.
Put in a value
and switch the box's kind
to see if it matches.

Number box
Still empty

Check whether the box's kind and the value match.

Setting a kind
prevents mistakes
from a wrong value going in.
Numbers in a number box,
text in a text box.
Each value has a kind,
and keeping that kind
is a basic of programming.
Next time we'll make
a fork in the road
with this true·false.

In one lineValues have kinds (number, text, true·false), and the kind decides what can be done with them. "3" and 3 look the same but are different values. One quote mark splits calculation from sticking together.
Was this helpful? Support seegongsik
Programming