Variable Introduction
Variables is where the computer stores information. As programmers, we are able to use variables to hold different kinds of information.
It is easiest to think of a variable as a "bucket" that has a place where the information is stored.
Variable Types
In Turing, there are 4 different types of variables.
Integer
Called "int" for short, integers are all positive and negative whole numbers.
…, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, …
Real
Like integers, Real numbers can be positive or negative. However, real numbers can have a decimal point. This is especially handy when you need to represent a fraction. (Such as 0.5).
…, -5.5, …, -1.2, …, 0.0, …, 4.3, …, 6.0, …
Note that even if the real value is a whole number, like 6, it must still be represented with a decimal, to make 6.0.
Strings
A string is just that, a 'string' of characters. This is a very important type of variable, as it allows us to show text to the user. All strings are surrounded in double-quotes ("…") in order for Turing to tell the difference between normal text and String text
sName := "Bob" is not the same as sName := Bob
Boolean
A boolean variable is a variable that has one of two answers. Boolean variables can be "true" or "false". That is it. While it does not seem they are very important, IF Statements and Loops use them very often.
Declaring Variables
In Turing, to declare a variable, we type:
var <VariableName> : <VariableType>
An example would be:
var iNumber : int
This would create a variable (bucket) called "iNumber". It would be of type integer.
Keep in mind that Turing is case sensitive. That means
put sname
is not the same as
put sName
Declaring Constants
Declaring constants is very similar to declaring variables, except that you tell Turing what the value of the constant is when it is created. To create a constant, you type:
const <ConstantName> : <VariableType> := <ConstantValue>
An example would be:
const PI : real := 3.14159
Keep in mind that you cannot change the value of the constant after you have created it. This includes using "get" or ":=". Think of it as a jar with a lid on it. Once the lid is on (After you've created it, and gave it a value), the lid cannot come off.
Assignment
Value Assignment
In order to place a value in a variable (Putting something into the bucket), you can use the assignment statement. To assign a value to a variable, you use the ' := ' operator. The general formula looks like this:
<VariableName> := <Value or Expression>
You must make sure that the Value you want to place into your variable is of the same type as the variable. The code:
rNumber := 3.14159
works fine because the variable is a real, and so is the value we place into it. But if we tried:
iNumber := 3.14159
This would not work, since we want to put a real value into an integer variable.
Expression Assignment
As we touched on before, we can also use Expressions on the right-hand side of the Assignment statement. These expressions must be made up of mathematical operators. An example of using an expression in an Assignment statement would be:
iNumber := iNumber + 1
However, if we tried:
iNumber + 1 := iNumber
We would get errors, because our expression is on the wrong side of the Assignment statement.