Variables

We took a brief look earlier about variables and printing them. This chapter concentrates on manipulating them and variable types.

You can change a variable by simply putting:

Set apples to 3 or
apples = 3

But what happens when we want to add, subtract, divide or multiply the number stored?

Add 4 to apples or
apples = apples +4

The pseudo code here is pretty self explanitory but the standard code states that: take the number in apples and add 4. Print apples will now output 7. The same can be applied to subtraction and others:

apples =4
apples = apples – 2
apples = apples * 4
apples = apples / 8

print apples

Output > 1

The code above subtracts 2 from the original 4, multiples by 4 then divides by 8 leaving 1. You can make these equations as complex as you like but it’s important to remember computers use standard maths rules and will multiply and divide numbers first, so always use brackets when possible.

If 3 people have 2 apples and each person buys another 2. How many apples are there in total?

What’s the answer? 12

Did you get this using 2+2*3?

2 + 2 * 3  is not 12

2 + 2 * 3 = 8    —–> CORRECT

The computer will multiply first so writing this is identical to writing 2+(2*3). You therefore need to bracket the 2+2.

(2 + 2) * 3

So written in code would look something like:

apples = 2
people = 3

print (apples + 2) * people

Output > 12

If you left out the brackets the output would be 8.

Variable Types

So far we’ve only looked at whole numbers but what happens if we want decimal numbers and what would happen if we put a word in the apples variable?

Here we’ll look at an important part of variables which is variable type. Variable types come in many forms with the most common being integer, doubles and strings.

Integer: Usually shortened to ‘int’. An integer is a whole number. Integer variables cannot have decimal places eg -5, 3, 3276.

Double: Doubles are the usually the largest variable type in terms of bits they can store. They are used to store decimal numbers eg 5.34, -100.5678, 400012.87266723.

String: A string variable holds words and letters. They usually cannot be manipulated in the same way ints and doubles can unless converted but are excellent at storing any string of characters without error.

As we mentioned in Chapter 1 you declare and initialise variables. How you do this depends on the langauge you’re programming in. You cannot mix variable types and so the example below would throw an error. Shows psuedo code followed by standard code.

Set apples as integer equal to 3

Set apples to 4.5

Output > Error cannot add double to integer

or

int apples = 3

apples = 4.5

Output > Error type mismatch

The ‘error type mismatch’ is an error output shown on many langauges and will usually point to apples=3.5. Variable types are usually the one thing most beginners struggle to get to grips with especially when equations mix types. You can convert variable types by typecasting a variable. ‘Typecasting’ is when you convert a variable to a format you want. If we look at the example above again:

Set apples as integer equal to 3

Convert 4.5 to integer and set apples to this number.

or

int apples = 3

apples = convertToInteger(4.5)

The output now would be 4.5? No

Even though this typecasting doesn’t return an error, you’re converting the number to an integer that cannot have decimal places. So in the majority of cases the computer simply removes all numbers after the decimal point therefore printing this variable would output 4.

I bet you’re wondering if you can convert a string? No probably not but you can on some languages if the string is a number.

int apples = 3

string oranges = “6″

string name = “mark”

apples = apples + convertToInteger(name)   > error thrown

apples = apples + convertToInteger(oranges)

print apples

Output> 9

In many languages the typecast is simply written: apples = apples + (int) oranges

This concludes out tutorial on variables. Next week we’ll look at sequences, conditionals and iteration.

Leave a Reply

You must be logged in to post a comment.