As humans, we’re able to differentiate between 12
, a number, and hello
, a word written with letters. However, computers can’t work this out on their own. At least, not without some help from us—we have to tell the computer what type of value it’s dealing with so it can read and understand it. Providing the type of a variable gives the computer the information it needs on a number of parameters, such as:
Its size
The memory layout of the variable
The range of values that can be stored in the memory
The operations that can be applied to the variable
A type is a data classification which tells the computer how the programmer wants to use the data.
There are many types of variables, and so far we’ve only come across one of them—numbers. But other types of data can also be used, such as words and booleans, two types which are frequently used in programming.
You can’t divide a word by five, for example, or put a number in capitals! But computers can’t tell the difference between the two data types, and this is why we have to assign types to the different concepts we use in a program.
Think of it like a LEGO set. Some pieces are flat and wide for building the base of a structure, and others are round to make a mast or door hinges, etc. Certain pieces have specific uses, but they’re all LEGO!
Learn About Numbers
Just like in math, there are different sorts of numbers, which can be integers (with no decimal point) or floats (with a decimal point).
Integers: 1, 5, 10. Decimals: 1.3, 1.7777, 2.3.
Learn About Strings
A string is made up of a set of characters, which can include spaces and figures. For example, “a,” “Algorithm,” and “I have two brothers.”
Learn About Booleans
Last, but not least, we have a type of data which provides a “True” or “False” answer. Think of it like a switch, or the two sides of a coin.
“But what’s the use of that?” I hear you ask.
To use conditions, of course! When you check a condition, the information provided as a response is a boolean type.
You can now add this information in the variables declaration section, adding the type after the variable.
INTEGER | A whole number |
FLOAT | A decimal |
STRING | A string |
BOOL | A boolean |
This is what it looks like in the pseudocode:
Type algorithm
Variable
wholeNumber ← 0 : INTEGER
floatingPointNumber ← 0.0 : FLOAT
string ← “” : STRING
boolean ← False : BOOL
As you can see, not much changes in the declaration of variables—all you need to do is write a colon :
and then the type of the variable.
Over to You!
Context
This exercise is based on the following algorithm, which calls the moveStartEnd
function, which moves the player from Start to End automatically:
Maze algorithm
Start
moveStartEnd()
End
Instructions
You’ll need to create two variables:
The first saves the name of the player.
The second states whether the player has finished the maze.
Also, the moveStartEnd
function needs to take the name of the player as a parameter to work properly.
Your Mission
Declare the
playerName
variable, initialize it, and add the type of data.Declare the
maze_completed
variable, initialize it to false, and add the type of data.At the start of the algorithm, assign your name to the
playerName
variable.Add the
playerName
variable as a parameter of themoveStartEnd
function.Change the value of the
maze_completed
variable if necessary in the algorithm.
Check Your Work
Here’s what the result of this exercise should look like:
Maze algorithm
Variable
playerName ← "" : STRING
maze_completed ← False : BOOL
Start
playerName ← "Kurt"
moveStartEnd(playerName)
maze_completed ← True
End
Let’s Recap!
A variable can be considered as a memory location that can contain values of a specific type.
Each variable has a specific type, which corresponds to the type of data it can contain.
There are different types for storing numbers, strings, and booleans.
The type of a variable can be provided in the
variable
section of the pseudocode to provide more information on the variable.
Now you’re familiar with the different types of data, you’ll never assign the wrong type to your variables again! This is crucial for the next bit of the program, and especially when it comes to what you want to do with these variables. In the next chapter, we’ll see how to determine whether certain instructions are launched or not, using conditional structures.