Show Index

Now on to variables.  So what are variables?  Variables are containers of data.  You, as the programmer, can use variables to temporarily store different bits of data.  This is a very important aspect of computer programming.

Let me take a second to note that any time you learn something entirely new, you may be lost at first, and perhaps have trouble following or understanding the concepts and terminology used.  However, if you just tread through it a ways, some things may start to make sense.  You may find as well that returning to this point and rereading this section will be much easier the second time through.  So bear with me for a bit...

Variables have names to differentiate themselves from other variables.

You can name a variable almost anything you want, such as:
variable, variable2, apple, answer, lljwjdiisen3mm, AVeryLongNameThatUsesCapsSoYouCanReadIt, Q, Jamie, tree, etc.
as long as the name of the variable meets these three conditions:

  1. it starts with a letter,

  2. it has no spaces, or special characters

  3. it isn't the same as a word already used by the BASIC language (such as Print, or Delay).

Variables usually store 1 of 3 types of data:

  1. An integer number (a number that has no decimal digits; i.e. 1, 17, -3, 20456, etc.)

  2. A "floating point" number (a number that has decimal digits; i.e. 1.0, 17.2, -3.002, 20456.2342456633, etc.)

  3. A "string" (a collection of letters/numbers/characters, in other words, text; i.e, "Hello", "My name is Ron", "The answer is 4.")

    TECHNICAL INFO (feel free to skip for now if it's overwhelming):
    In BlitzBasic, integer variables are any variables that aren't explicitly designated as one of the other variables.  Integer variables can also be identified by following the variable name with the percent (%) symbol (although it's not necessary to do so).

    For example, if we created a variable and named it answer.  It would be assumed to be an integer type of variable.

    To try to prevent confusion, when referring to the variable named answer, I will italicize the name of the variable

    We could also call it answer% (note the percent sign at the end of the variable name).  It would still be an integer type variable.  This variable that we call answer (or answer%) can be used to store an integer type of number such as 1 or 0 or -23 or even 1,340,230,999.

    In Blitz, floating point variables are variables whose names are followed by a pound (#) symbol.  So taking our answer variable example from above, if we named the variable answer#, it would now be able to contain floating point numbers such as 1.00 or 0.00023 or -17.5.

    And also in Blitz, string variables are designated by following the variable name with the dollar ($) symbol.  So, once again, taking our answer variable example, if we named the variable to answer$, it would be able to contain 'strings' such as "basic" or "My name is Ron!" or "Programming Tutorial #1" or "42" (note that even numbers can be stored as strings).

    One last note about BlitzBasic.  If a variable is defined as one type somewhere in your code, it cannot be redefined as another type elsewhere in your code.,  So, for example, if you were to name a variable say, var17# (defining the variable as a floating point type of variable) then you couldn't use the same variable name elsewhere in your code as a string type of variable (var17%).  Got it? :)

So, why do variables store different types of data?  This is so that the computer knows what the data contained in the variable means.  Internally, all data in the computer is stored as 1's and 0's so the computer really can't tell the difference between one kind of data and another.  The programmer needs to tell the computer what kind of data is stored in each variable.


By this time, you are probably asking yourself what variables are good for.  You now know that they can contain data, but what good is that?  What do you do with the data?

The computer uses variables to store whatever information you need it to store.   Let's say that you wanted to perform a math operation like so:

2 + 3 =

You could simply enter this problem into Blitz and it would tell us the answer:

Print 2 + 3
Note that you can type the above code into a new page or just erase the code you typed earlier and then type in the above code.

which would produce the result:


But now let's say that you wanted to take that answer and multiply it by 2.  We would need a way to temporarily store that answer.  We could do that by using a variable.  Let's use a variable that we will call 'answer' to temporarily store the result of the first math operation.  (Note that because 'answer' isn't followed by # or $ (answer# or answer$) that it will be an integer variable).

Here's how you would type it in:

again, you can either start a new page or just write over the old code with the following code
answer = 2 + 3
Print answer
Print answer * 2

answer is the name of our variable that we are using to store the result of the math operation (2+3).
On the first line, answer is assigned the value of 5 (the result of the math operation)
On the second line, the value stored in the variable answer (5) is printed to the screen.

NOTE that Print answer is different than typing in Print "answer" (note the quotation marks), which would actually print the word "answer" to the screen.

On the third line, the value stored in answer is multiplied by 2 (5x2) and then the result (10) is printed to the screen.

Just in case you weren't aware of it, computers use the asterick symbol * to signify multiplication (not an x!).  They also use the forward slash symbol / to signify division.

all of which would produce the results:



Now here's another example of when you would need to use a variable.  Let's say that you wanted to ask the user what his/her name was and then tell them "Hello" using their name.

First, we have to ask for the users name and store it in a variable.  We can use the Input command to ask for their name.
The Input command works like the Print command except that it also allows you to type something into the computer.

A quick note about Blitz:  If you ever want to know more information about a BASIC command, place the cursor over the command and press the F1 key once.  A short description of how the command is used will show up in the Status Bar.  If you press the F1 key a second time, the Help file will automatically open and show you a detailed explanation of how to use the command.  Isn't Blitz Great?  OK, back to the tutorial...

Second, we have to print the word "Hello " and then their name to the screen.

Lets create a variable and name it name$.  Remember, the $ tells the computer that the variable contains a string (some text).  This is necessary of course because we are going to use the variable to store a persons name.

Here's how you would type it in:

name$ = Input("What is your name? ")
Print "Hello " + name$

On the first line three different things happen (and in this order...).  The user is prompted with the question, "What is your name? ".  The user is allowed to type in their name (they have to press enter when finished typing in their name).   The variable name$ is then assigned the text that the user typed into the computer.
On the second line the computer prints to the screen "Hello " plus the text that was previously stored in the variable name$ (i.e. the persons name)

NOTE that adding two text strings together ("Hello " + name$) doesn't actually ADD them like numbers, it just prints one after the other.

which, if the user typed in that their name was, say "John", would produce the following results:

What is your name? John
Hello John


Continue on to Conditional statements.
Goto next lesson


If you've reached this page and there's no index on the left, Click here to show the Index Page