Begin Programming Logo
Home | KPL | Object Pascal | General Programming | Code Database | About Us
   
Variables
   
   

Variables are possibly the most important bit of computer programming.  Variables are simply places in memory where data can be stored. Each variable is assigned a name and a type. You use the variable name to tell the computer where to store the value.

Before you can use a variable in KPL, you must declare it.  This simply means telling KPL that you  are going to use a variable and its name is…….. and it will store…… To do this, KPL uses the following code

Define Count As Int

Let’s look at this in more detail; ‘Define’ is simply the word which tells KPL that this line of code is a variable declaration.  ‘Count’ is the name of the variable…just like with sprites, we give each variable a meaningful name which describes what it will do.  Int is short for integer, which means whole number.  This tells KPL what sort of data we are going to use this variable for.  When you declare a variable of a certain type (eg declare Count As Int) you cannot use another type of data in that variable (just like you could never store image data in an MP3 file!).

 So we now have an area of memory in our computer setup, called ‘Count’ which will store an integer (a whole number).  This is like an empty envelope which we can put a letter in.  We will now write in our code the following

Count = 1

This means that the variable 1 becomes equal to 1.  Another difference between maths and computer science is that in some languages (including KPL), = does not mean is the same as, infact, = means becomes equal to.

For example, a maths teacher would say the following is wrong

X = X + 3

Because we can subtract ‘X’ from both sides and so say 0 = 3, which is obviously wrong, but in computing this would mean ‘take the current value of X, add 3 to it and store the result back in X.

We can also use Print() to display the current value of a variable, so to our program, write:

Print(Count)

We can now change the value in our variable as follows:

Count = Count + 1
Print(Count)

If you add this to your program, you will find that your program now says 1 and then 2.  You can do ‘maths’ with the following operands [+, -, *, /]

Exercises

  • Use variables of type Int, to find (23 + 41) * 12 and print the result on the screen
  • Use variables of type Int, to find (243 - 76) * 27 and print the result on the screen

So far we have declared a variable and then assigned a value to it (called assignment).  Another use of variables is in iteration.  Imagine the following code:
Source code showing a assignment
Here, we declare a variable, called Count of type integer.  We set its value to 1.  We then create a ‘while loop’ so that while the value stored in the variable Count is less than 20, it keeps repeating whatever is between ‘While’ and ‘End While’.  If you look inside the while loop, you can see that Count = Count + 1, therefore each time the loop is run, the value of Count increases by 1, until Count = 20.

By doing this we are using a variable to repeat a section of code a number of times, by counting how many times we have done it using a variable.  This allows us to save a lot of copying and pasting of code.
                  
The process of using a variable in a loop to repeat code a certain number of times is called iteration.

Exercises

  • Print the numbers from 1 to 1000 on the screen using iteration

 

In KPL, there exists a type of iteration called a ‘for loop’.  This is very similar to using a while loop as above, however it has some slight differences in when you can use it.

With a while loop, you have to use 3 lines of code to setup the loop; the initial value of ‘Count’, the while condition (eg while Count <20) and inside the loop you need to put Count := Count + 1.  With a for loop, all 3 of these lines are condensed into a single line.  The command looks like this:

For Count = 1 To 100
            Print(Count)
Next

The line ‘For Count = 1 To 100’ sets the initial value to Count to 1 and sets the final value as 100.  The step is always 1, therefore this code will be repeated 100 times.

For loops must have fixed initial and final values.  They also cannot be used if you don’t want the step to be 1.

For example, if you want to keep adding sprites to your game until the game is finished, you would have to use a while loop as there is no fixed length of the game.  If you wanted to have a loop so that you would repeat code when Count is less than 100 and Count would increase by 7 each time the loop is run, you would have to use a while loop as for loop has a fixed incremental increase of 1.

Exercises

  • Print the numbers from 1 to 1000 on the screen using a for loop
  • Draw a line from (0,0) to (200,200) using the pen, so that you draw the line one co-ordinate at a time, using a for loop.

So far we have assigned values to variables (called assignment) and used variables to repeat code (iteration).  The final thing we can do with variables is use them to say ‘if a variable = some value then do some code’.  This is called selection.

Imagine the following example:
Source code showing a selection
What happens here is we tell KPL that Count = 7.  On the next line we say ‘If Count = 10 Then’, well we know Count = 7, therefore Count doesn’t equal 10!  KPL therefore does not process the code between ‘Then’ and ‘End If’

Notice that we have indented the code between ‘If’ and ‘End If’.  This is because that code effectively “belongs” to the if statement. It shows us by looking at the program, without even reading through the code, looking for ‘End If’ the code which will be run if the if statement is true.  Although it may look obvious what is happening here, if you write a commercial game, you will write ‘if’ statements where the ‘Then’ and ‘End if’ will be hundreds or thousands of lines apart with various other selections and iterations inside the if loop and if you were to miss out a single ‘End If’, it could take hours to find if you had not used good indentation.

One of the powerful things about variables is the way in which loops can contain more loops, which contain if statements, which contain more loops.  This is all called nesting.  For example, the following will draw a line of different colours:
Source Code showing how to draw a line of many colours
So far we have only use one type of variable, Int, however KPL also has other types of variable as listed below:

Int Whole number (can be both positive and negative)

String

Stores text

Bool Short for boolean. Stores 1 or 2 values; so can either be True or False
Decimal Like Int, however can store a number with decimal places (eg 3.1415926)
   

Valid XHTML 1.0 Transitional Valid CSS!