Computersight > Programming > Blitz

Variables in Blitz

A short tutorial on variables in BlitzBasic.

Page 1 of 2 | Prev 12Next»

Variables:

What is a variable? Well, a variable can be a lot of different things, but in Blitz a variable is a word that has a number or string assigned to it. A string is anything enclosed in “”. In other words “Good Golly it Works” is a string.

How do you assign variables? Well its actually very simple. It works something like this.

age% = 14
meters# = 5.6
expression$ = “Good Golly it Works”

Now those words can be used in the place of what their assigned too, and you can now change what their assigned too. For example:

expression$ = “Good Golly it Works”

Print expression$
Delay 5000

This program is exactly the same as the first one.

Now you might think this is pointless, in this program it is, but what if you have to use a number a 100 times. If you use a variable, then you don't have to go through the entire program to fix all 100 numbers, you just change the variable.

Variables however aren't just used to save you time, you can also use them because they can change, and change is how your game works. Take this for example.

Lives = 1
Lives = Lives + 1
Print Lives
Delay 3000

As you can see, the second line changed lives by adding one. You can do this with -,*, and / as well. There is some more advanced math too but that's in the next chapter.

At this point I would like to go over some different types of variables.

First there are Global and Local variables. The variable we declared in the previous program was Local. A local variable will only work in the main program, and will not work in functions, which are later in the book. It also works vice versa, where a Local variable in a function only works in the function.

When you are declaring a Global variable, you simply declare it like this.

Global Lives = 1

Just the variable name automatically makes it Local, however in a function you must call the variable as.

Local Lives = 1

And again we will go over functions later.

There are also constants; these involve declaring a variable that must stay the same. They are declared.

Const Lives = 1

Well, that's about it for variables. Let's move on to the “if” statement.

The “If” Statement:

The “If” statement is the bulk of most of your programs. It allows you to say if something happens, do something. For example you might say in your game.

If player hits up,
Move the character up

And this is all very good and fine, except that if you tried to compile the above lines, you would get a very large amount of errors. So how would you write the above bit in Blitz.

If KeyHit(200) Then
playery = playery – 5
EndIf

For write now, I would like you to ignore the KeyHit command, but concentrate more on the “if” part. As you can see, it started with an “If”, all statements start with an If. Then you put in your tester. So in this case KeyHit(200) is your tester. Then comes the “Then”, this is not required; the following code is perfectly legal.

If KeyHit(200)
playery = playery – 5
EndIf

However, the code with the “Then” is usually a bit easier to read.

All “if” statements must end with and “EndIf”, if not it will produce an error. This can become very tricky also when you start nesting your “if” statements, nesting means you put an if statement inside another, for example.

If KeyHit(57) ;spacebar
If ammo > 0


;shoot bullets
EndIf
EndIf
Now there are about three new concepts here, so let's go over them one at a time.
First let's talk about commenting, commenting is the part of your program that only humans can read, the computer can not read it. To write a comment simply use a semi-colon before you write the comment, anything after the ; is not included in your program when you run it, but any person who looks at your code can see them. It is a good habit to write yourself comments even in small programs because when they get big, you need comments.
Next let's talk about the nesting in the previous example above. As you can see each “If” had a corresponding “EndIf” with it. It is a good idea to line up each “If” and “EndIf” so you don't leave one out.
Finally I would like to talk about operators. We already went over a few in the variables section. These were mathematical operators, they included +,-,*,and /. However, there are also the relational and logical operators.
Relational Operators are the main power for your “If” statements, they include <,>,=,<=,>=, and <>. <> stands for not equal too. You use these statements to compare variables and see if something should or should not be done. Example.

Page 1 of 2 | Prev 12Next»
0
Liked It
I Like It!
Related Articles
Arrays in Blitz  |  Loops in Blitz
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.