GFAvariablestutorial

From Atari Wiki
Revision as of 14:55, 1 October 2006 by Simonsunnyboy (talk | contribs)
Jump to navigation Jump to search

Variables and data types

Welcome to the second chapter of my little tutorial. Today you'll write a few small programs that will do some simple calculations for you. First let's learn about variables and how the computer deals with numbers. Variables

As you probably already know, computers deal with numbers all around. All sort of data is encoded into numbers. Even this text you're reading or any text you enter into GFABASIC consists of all little numbers. To be able to talk to the computer, you must know how to talk with the computer numberwise. Let's begin with a small example on number usage. No need to worry - all makes sense fast, just keep in mind that anything is a number. Imagine you want your computer to calculate a little equation for you. Let's assume the computer shall calculate the value of 5+5. An easy thing but soon you will want the computer to store such results for later use. And perhaps you will even want the computer not to add 5 and 5, but perhaps other numbers that you want to type in. So you must find a way to tell the computer where it can store results of computations and data in general. At this point socalled variables drop in. Think of a large cupboard with lots of little drawers in. Each drawer holds some data and has a name. Think of variables as if they were one of those little drawers in your cupboard. You can place your data in them and retrieve it by name. Two problems need to be solved: you want an easy way to access your data and secondly the computer needs to know where to place this data. Luckily GFABASIC takes care of that. You simply give a variable a name and so specify the drawer where to place the value of the variable. You can then refer to it by giving its name in the place you want the value of the variable to be used. GFABASIC will automatically know where to store the data and how to deal with it. It is also important to tell GFABASIC what sort of data you want to store. You can have variables that store only numbers but also variables that deal with a character or a whole string, a line of text for example. The following valid line of GFABASIC code will create a variable called x for you and assign it the value of 10.

x=10

You learn here 3 important things. First your variable has a name, x in this case, secondly you state that you want to do something with this variable, an assignment with the = sign, the last thing is what you want to do with this variable. You give it the value 10 here. Such an assignment will overwrite any old data that has been stored before in that variable. As long as you don't assign a value to a variable, it will hold a default value, 0 in most cases.

Data types

Now how you see that this variable x stores a number? How does GFABASIC know that you want to deal with a number? Easy - by the way the name of the variable has been written. To distinguish between different ways of data types GFABASIC appends a special typing sign as a suffix to the variable name to distinguish between several ways to store data in variables. It is important that variables with a special suffix are different from those without. The main data types you'll use in this tutorial are described in the following table. basic data types in GFABASIC variable types

basic data types in GFABASIC
variable

types

suffix
example
usage
floating point number, short called float , 1,5 for example
none
x
in calculations where the fractional part of a number is

important

integer (4 bytes long)
%
y%
everywhere where your numbers are always even or odd, without

fractional (e.q. 5 instead of 5,5) Please note that a result from a float operation will be stripped of its fractional if you save it to an integer variable.

string
$
title$
use strings when you want to deal with textual data instead

of numbers

boolean, used for truth values with only two values, TRUE and

FALSE

!
flag!
to store truth values or results of boolean expressions or to

set flags

GFABASIC can distinguish even more types of variables but I'll leave it up to the GFABASIC manual to tell you about them. You can solve 99% of all problems with the types above.

x=10.56
y%=4		
stop_execution!=FALSE	
txt$="a short string"

This short piece of code creates 4 different variables of 4 different types and assigns values to them. As you can see it is fully valid to define a variable without a suffix. This will make it a floating point variable by default. Both types for storing numbers deal with the sign so the following is ofcourse valid in GFABASIC:

x=-15.4
y%=-4

More on strings and boolean variables later.

Variable naming

A short intermediate step is to tell you what names you can use for your variables. You can use all letters and numbers for your variable names. Spaces are not allowed but underscores inside the variable name. In general GFABASIC can distinguish upto 16 characters per name. GFABASIC limits you only in the following ways: a variable may not begin with a number or an underscore, only with letters. Avoid to name your variables like GFABASIC commands. It will work but it can also cause troubles while typing. As a rule, never use a GFABASIC command as a variable name. Valid variable names look like the following: x, auto%, lives%, bonus1%, x_1!, city_name$, debit Invalid variable names look like this and GFABASIC will complain: _blank, 1x, ?value%, 5s$, :invalid_variable_name Always remember: begin your variable names with a letter from A-Z and you are on the safe side! Arithmetics and calculations Now that you know how you can tell the computer to store numbers in its memory and how to give them a meaningful name, you'll learn how to calculate with them. You remember my introductory example of adding 5 and 5? Well, let GFABASIC do that:

x=5+5
PRINT x

This will take 5 and 5, add both together and store the value in the variable x. The value of the addition will then be written on the screen. Having stored the value you can recall it at any time by using the variable x in a useful context like an expression or calculation. Ofcourse you could have made this program a bit shorter by writing directly:

PRINT 5+5

But remember the educational value of the first one. The computer remembered the result of the calculation in the variable x so you can reuse it later in your program.

As you probably already guessed, GFABASIC will execute your program beginning with the first line. You will learn later on how to alter the execution flow. As a little exercise, switch both lines around and explain the result to yourself!

Ofcourse you are not limited to add plain numbers together. You can ofcourse use variables as well so try this:

x=6
y=3
z=x+y
PRINT z

As you probably already guessed, this is the key to reuse your calculation results. The part behind the = in the assignment line is called an expression. It can contain brackets and functions as well. Later on you'll be able to use such expressions as x+y in other contexts as well. A valid expression would be:

result=150/(x-(y*15.5))

Brackets control the order of execution. As GFABASIC does not stricly calculate multiplication before addition, you sometimes need to set explicit brackets that would be unnecessary in an algebra lesson. But always remember: an additional pair of brackets will make your expression more clear. So it is not bad if you have unnecessary brackets in your expressions.

Ofcourse you can use the old value of the same variable in an assignment to calculate its new value. Imagine you have a variable called multiplicator% that holds the value 1 and you want to add 2 to this:

multiplicator%=1
PRINT multiplicator%
multiplicator%=multiplicator%+2
PRINT multiplicator%

You see it is rather easy to understand. Now experiment a bit. Let the ST do some calculations for you.

Progress check

As this chapter explained vital things for programming with GFABASIC, you should answer the following questions to yourself. If you still have problems, read the chapter again.

  1. You want to assign the value 5 to a variable called joe. What code do you need to type into GFABASIC?
  2. What's the difference between a variable called x and one called x%? Is there any?
  3. Variable names ending with ! have the data type ... . They store .... .
  4. What number would be contained in the variable x% after execution of the following code ? x%=1.5
  5. Which of the following variable names are valid in GFABASIC? x%, y!, 45x, ublank_$, $$, $_, city$, xdebit, flag!
  6. Write a little program that will add 3 float numbers and divides them through 70. Write the result on the screen.

This closes our second chapter. You have learned by now how to create and name variables in your own program. You should now know what values to store in what type of variable and how to do simple arithmetic with them. The term expression is not unknown to you and you are able to write multiline programs with the GFABASIC editor.

back to the GFA Tutorial