Variable Tutorial II

Variables in FreeBASIC Tutorial II

FreeBASIC was compatible with old forms of syntax before compiler release .17. Today, FreeBASIC supports new way to write BASIC syntax. In the first variable tutorial, you learned how most of modern BASIC compilers handles variables.

Back to beginning: Creating Variables with Dim

You create variables with Dim this way :

Dim variableName As DataType

So, if you wanted to create a string variable named cName, you would use :

Dim cName As String

When you create variables with Dim, the data type sign is omitted. Here's an example of the types of variables you have learned so far.

Dim cName As String
Dim hitPoints As Integer
Dim speed as Single

cName = "Paul"
hitPoints = 100
speed = 0.5

Print cName
Print hitPoints
Print speed

Sleep
End

Note that the floating point number variable type is Single. You'll learn why in later tutorials, but for now it's enough to know that variables Dimmed as Single hold numbers with decimal points.

Changing type of variable: Can i?

Pretty often there comes a situation where you have some temp variable wich you would like to use in your programs as an handyman.

Let's say you want to ask questions from a player, and you would like to change type of variable between questions.

DIM AS Integer points = 0
DIM AS String answer
INPUT "What is the capital of Finland"; answer
IF answer = UCASE("helsinki") Then
    points = (points + 1)
END IF

' here we change type of //answer// since we are asking question where answer is INTEGER
REDIM AS Integer answer
INPUT "How much is 3 + (3 * 3)"; answer
IF answer = 12 THEN
    points = (points + 1)
END IF

' and again, we change type of variable. 
REDIM AS Double
INPUT "How much is 22.2 - 11.1"; answer
IF answer = 11.1 THEN
    points = (points + 1)
END IF

Print "You got "; points ; " points."
Print "Thank you for playing with me ;) See you next time."

Names of variables: Does it matter?

Yes, it does matter if your code is longer than few lines.
Imagine a some big programming project, something like a FireFox web-browser where is tens- if not even hundreds of thousands lines of code. If programmers would not use some good and planned variable names, could anyone read and understand that code at all?

Here is some examples how to name variables. This is not the best way, but its how i like to name my variables.

Counter variables
If i need to do some looping, ill use "Counter" as name. If i do lop inside of loop, i do have idea for that too.

DIM AS Integer CounterA, CounterB
For CounterA = 1 TO 100
    For CounterB = 1 TO 100
        Print CounterB
    Next CounterB
Next CounterA

Easy, eh? If you need to save name of player, age of player and so on, use your imagination.
DIM AS String Plr_Name
DIM AS Integer Plr_Age

Input "What is your name"; Plr_Name
Input "How old are you"; Plr_Age

In next tutorial we do create our own data types wich does save your time, nerves and lines of code.

page tags: tutorial variable
page_revision: 1, last_edited: 1209000047|%e %b %Y, %H:%M %Z (%O ago)