A Course in FreeBasic: Chapter 1..

Welcome to FreeBasic!

FreeBasic is the next level for the Basic language. This few month old language is better than QBasic while keeping 99% of basic's statements. FreeBasic takes Basic to 32 bit, therefor shedding of the few 16bit statements. All the while, its free to access and use, and gains a few new statements that make it easier to use than QBasic.

FreeBasic is a commandline compiler, thus to write and compile you only need NotePad, and some knowledge of using the comandline. To do this: Write a basic code, save it a .BAS, open your commandline, type in the location of your FreeBasic compiler (FBC.exe), then when it runs, type in the location of you .BAS file. Another trick is to use a .BAT. For example, open Notepad and type:

C:\FreeBasic\FBC.exe C:\FreeBasic\Program.bas
Then save this as .BAT, and run it. NOTE: Locations might be different!

If that doesn't sound like you type of thing, don't worry!! Just like QBasic, IDEs are available to run it. Swing over to SourceForge FB-IDE. And get the FBIDE + FBC package. With this, you simply write the code in the ide, press F5, and it does the rest! But, if the commandline sounds like your type of thing, then get the plane FBC here. I suggest the IDE for beginners.

No more waiting,..

Before this gets boring, lets try some coding. We'll go over PRINT, INPUT, CLS, SLEEP, and REMs. Here's a FreeBasic code:

'This is a FreeBasic
code!
PRINT "Hello, world!"
SLEEP 1000
CLS
PRINT "I'm a computer!"
SLEEP
Now, the first line does nothing. It is skipped because it was REMed by a '. The word "REM" can also be used instead of the '. These are better know as comments, and are very helpful when reading over your code. Think of them as lil name tags if you like.

The next line, this is used to display text on the screen. After PRINT, any thing between the " "s or inserted behind it as a variable/constant(We'll get to those later) is printed to the screen. NOTE: PRINT displays text on your monitor screen, it doesn't actually print anything from your printer.

SLEEP? You ask. Yep, just as it sounds, this statement puts your program on snooze for a period of time, or until a key is pressed. In this line of code, SLEEP is given a value equal to 1 second (1000 = 1second, 2000 = 2seconds, ect). Play with this setting if you like, if you set it to high, a keypress will awake your code once more.. so, you won't have to wait for hours. ;)

Moving on, CLS, this stands for: CLear Screen. This wipes clean every thing on the screen. Once this is done, your back to your original black screen, ready for new stuff to be put on it!

Since you know what the next line is, I'll explain the last one. This is a undefined SLEEP, most important in this code. FreeBasic programs run and then close when they are done. So in order to see the last stage, we need to place this type of SLEEP. With out a time limit on how long it can nap, it will continue to sleep until a key is pressed. Thus, the code is held up from closing, and we get to see: I'm a computer! : on the screen.

Okay.. Lets add on to our last code and try some INPUTs!
'This is a FreeBasic
code!
PRINT "Hello, world!"
SLEEP 1000
CLS
PRINT "I'm a computer!"
'NEW! INPUT
INPUT "What are you"; strng$
'NEW! Multiple
prints
PRINT "Cool, your a "; strng$; "!!!"
SLEEP
Focusing on the new lines of code (Ones with 'NEW above them), we'll examine this code. Before you can understand INPUT, you need a lil background in Variables and Constants. Variables are user defined names assigned with values, these values are allowed to change (Days, Years, ect). Constants are user defined names assigned values that don't change (Pi, YourName, ect).

Okay, now that is clear, The first new line is the INPUT statement. The included text after is printed to the screen just as it would with PRINT. After the ; is a Constant, given the program is asking you "what you are" which is always the same. If it asked your age, that would a Variable since next year it would be different. Also, the type of constant used is called a string, notice the $ at the end of it. This is for words, and any numbers inputted would be considered words and not actual numbers. (Variable/Constant types will be discussed in detail later)

You might have noticed I left out a ? in the displayed text. This is because the use of ; after it with the INPUT statement. This automatically puts a ? mark. If you ever come to a point where you don't want a ? mark at the end, replace the ; with a , .

Wow, this next line looks like the first, except it uses PRINT. With PRINT ; allows you to print multiple things to the screen. Notice that its given text to display, then after the ; , it will display the value of the constant that was assigned by the input statement. Then after the next ; it displays the next set of given text. Lets say you put Human in the INPUT, the output screen should look like:

I'm a computer!
What are you? Human
Cool, your a Human!!!

Debugging... When all goes wrong..

Well, thet’s is for chapter one and coding.. But, lets go over some debugging. Odds are, your going to have to do it, we are human, we make mistakes. Even just a simple typo can lead to disaster for your code. Using the above example, I'll simulate a few bugs, describe how to find them, ect...

'This is a FreeBasic
code!
PRINT "Hello, world!"
SLEEP 1000
CLS
PRINT "I'm a computer!"
'NEW! INPUT
INPT "What are
you";
strng$
'NEW! Multiple
prints
PRINT "Cool, your a "; strng$; "!!!"
SLEEP
Here is a simple mistake, and easy to find if your using a IDE with good error reporting. FB IDE will tell you:

Line| File            | Error | Message                        
 7  | C:\FreeBasic\.. | 10    | Expected '=' found 'What are you'
    |                 |       | INPT "What are you"; strg$
    |                 |       | ^
This makes it easy to locate that the error is on line 7, and that the problem is a typo.. For this code, a spelling is about all that can go wrong, except if you leave out a line of code:

'This is a FreeBasic
code!
PRINT "Hello, world!"
SLEEP 1000
CLS
PRINT "I'm a computer!"
'NEW! Multiple
prints
PRINT "Cool, your a "; strng$; "!!!"
SLEEP
Output:

I'm a computer!
Cool, your a !!!
In that case, watch your program run. When it stops doing what you want, go to the line of code before that bug, and see if any lines of code are missing afterward. As for the constant, you might mix the spelling:

'This is a FreeBasic
code!
PRINT "Hello, world!"
SLEEP 1000
CLS
PRINT "I'm a computer!"
'NEW! INPUT
INPUT "What are you"; strng$
'NEW! Multible
prints
PRINT "Cool, your a "; stg$; "!!!"
SLEEP
This is marked by a output screen like this when you run your code:

I'm a computer!
What are you? Human
Cool, your a !!!
Compared to the working code, this is not what you want. And naturally, this type of bug is a mismatched Constant or Variable.. Simply go back and check for misspells and typos..

Well.. that's that for this chapter.. Play with this some, and when chapter 2 is up, you'll be ready for some more codes and debugging tips & tricks.. If INPUTs, Variables, & Constants stump you in this tutorial, never fear! The next tutorial will explain there use in more detail! Hope you enjoy, and until next time, happy coding!!!

-Kevin (x.t.r.GRAPHICS) aka: Rattrapmax6