Datatypes & Variables
Information and data are the building blocks of logic
Types of Data
At its very core, CSP has three types of values: strings, numbers, and booleans. Each of these values has a very specific purpose when it comes to code.
Strings
A string is simply a chunk of text. As you saw in the last chapter, in order to display text we wrap that text in quotes. Wrapping text in quotes creates a string value.
Try to create your own string and display it back.
Under the hood, CSP works with strings as if they are not actually one value, but rather a list of many small values. If you strip away the abstractions. each character is its own value.
- Abstractions (noun)
- Simplified layers that hide the details underneath
You may have noticed that when you display multiple values, they appear on the same line. In order to move to the next line, we must print a new line. We do this with what is called the newline character. This may be compared to pressing enter/return on a keyboard. The newline character may be typed in one of two ways. First, we may literally have a string span multiple lines.
Second we may use what is called an escape sequence. An escape sequence is a special kind
of representation we can place within strings that turns into a different character when
CSP sees it. In this case, if we type \n CSP will not see \n but rather a newline.
- Escape Sequence (noun)
- A character pair like \n that stands for another character
There are other escape sequences also. See the table below for reference.
| character | sequence |
|---|---|
| newline | \n |
| tab | \t |
" | \" |
\ | \\ |
Numbers
A number is exactly what it sounds like, a mathematical value. We represent numbers using the digits 0-9. We may also have decimals in our numbers.
Numbers are especially useful because the ability to use arithmetic operators upon them.
In CSP the valid operators are addition (+), subtraction (-), multiplication (*),
and division (/). These are referred to as binary operators because they operate on two
different numbers at the same time. These operators can be combined into multiple
expressions; in that case, order of operations apply.
There is one additional operator that can be used on numbers called the modulus operator.
The modulus operator—defined in CSP by MOD—returns the remainder of a division. For
instance the remainder of 19 divided by 4 is 3; therefore, 19 MOD 4 returns 3. This
operator is especially useful when determining if a number is even or odd. If a number is
evenly divisible by two it is even and the remainder is 0. If a number is odd the
remainder is 1. 15 MOD 2 returns 1, so 15 is odd. 4 MOD 2 is 0, so 4 is even.
In regards to order of operations, MOD has the same precedence as multiplication and
division.
- Precedence (noun)
- Priority in regards to operations
Many programming languages use the percent sign as the modulus operator. See equivalent expressions to
DISPLAY(4 MOD 2)in various languages below.
Booleans
A boolean is the simplest type of value. It can only be one of two things: true or
false. Booleans are named after mathematician
George Boole,
and they are what allow programs to make decisions.
Notice that
trueandfalseare not wrapped in quotes."true"is a string (text that happens to spell a word), whiletrueis a boolean value.
Booleans can be combined using three logical operators: AND, OR, and NOT.
AND is true only if both sides are true.
OR is true if at least one side is true.
NOT flips a single boolean to its opposite.
The results of these operators can be summarized in the tables below.
| a | b | a AND b | a OR b |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
| a | NOT a |
|---|---|
| true | false |
| false | true |
Logical operators have an order of operations too. AND binds more closely than OR, in
the same way that multiplication binds more closely than addition. This means AND is
evaluated first, and then OR.
Here false AND false is evaluated first, giving false. Then true OR false gives
true. If we want to change the order, we can use parentheses, just like in arithmetic.
Now true OR false is evaluated first, giving true. Then true AND false gives false.
Many programming languages use different symbols for these operators. See equivalent expressions to
DISPLAY(true AND NOT false)below.
Variables
So far every value we have written has been used once and then forgotten. A variable lets us store a value and give it a name, so we can use it again later. You can think of a variable as a labeled box that holds a value.
We create a variable by writing the name, an arrow, and then the value we want to store. This is called assignment.
The arrow points from the value to the name. Here we store the number 10 in a variable
called age.
Once a variable has been created, we can use its name anywhere we would use the value itself. CSP replaces the name with whatever is stored inside.
Notice that age is not wrapped in quotes. DISPLAY("age") would display the text
age, while DISPLAY(age) displays the value stored in the variable.
In the editor, the arrow
←can be inserted by typing<-or by pressing the←button.
Variables can hold any type of value, including strings, numbers, and booleans.
Redefining Variables
As the word “variable” suggests, the value stored in a variable can change. Assigning to a name that already exists replaces the old value with the new one.
The first line displays 1, and the third line displays 2. The old value is gone.
A variable may even be used in the expression that replaces its own value. CSP always evaluates the right side of the arrow first, and only then stores the result.
On the second line, name + 45 is evaluated using the current value of name, which is
1, giving 46. Then 46 is stored back into name. This pattern of updating a
variable based on its own value shows up constantly in programming.
Most programming languages use an equals sign for assignment instead of an arrow. See examples below.
Next: Comments