Part 2!
First steps.
The time has come to learn some boring (
but essential) programming before we go onto the more fun stuff.
I am going to take a
procedural approach to this, some people would say you should take an
object orientated approach, others still would reccomend that you learn
assembly language so you can understand how a program is processed.
I will be taking the
procedural approach because it is simple enough to learn.
You do not need to know the difference between these conventions at the moment because all we are going to be doing is learning how to count, how to do aritmetic, how to store numbers and text.
I'll keep this to a minumum and then we can go onto drawing some stuff on the screen.
Lets get ready to write our first program..First of all we need to open our editor so go into the
FBEDIT directory and double click its icon to launch freebasic, we need to start a new program now so click on the "
file" tab at the top.
Then "
new code file" from the menu that drops down.
a window called "
untitled.bas" will appear, this is the window where we will be doing our coding.
From now on, everything you need to type I will be surrounding in
code boxes, you can highlight the things in these boxes and copy-paste into your editor if you want to save typing them yourself.
The first thing you need to know are
white space and
commentsThere are two ways of doing comments in your freebasic listings.
You can see them in the box below;
rem This is a comment
' And so is this!
You need to know that the
freebasic compiler
ignores comments and white space, white space is just blank lines or areas of blank text, you can
use white space to lay out your code neatly so it is readable, you would
use comments in your code so that you can see what your code is doing.
You should
always comment your code, the
rem command and the
' character are treated exactly the same by freebasic, they
are ignored!So the first thing that you learned does nothing but is the most important thing that you will learn today.
put as many comments into your programs as possible so you can go back at any time and see exactly what is going on.
So let's put some comments in our program, it's a good idea to put what the program is going to do in the comments.
' Program 01 "Hello World"
' Written by me on 17th July 2009.
' --------------------------------
You will see that fbedit colours these comments in
green when you type them in.
In fact it's pretty useful like that, it colours different parts of your program according to what they do to make it more easy for you to read it.
Here's the program you need to type then...
' Program 01 "Hello World"
' Written by me on 17th July 2009.
' --------------------------------
'Open an 800 pixel wide 600 pixel high screen
Screen 19,,1,,
'Print our message
Print "Hello World!"
'Wait 3 seconds (3000 milliseconds)
Sleep 3000
'Exit the program
End
There really is no time like the present so lets run it and see what happens..
First we need to save the file.
click "
file" on the top of the screen.
Then "
save as"
and call it:
helloworld.basYou'll notice that the tab has changed its name to "
helloworld.bas"
We need to do a couple of things to run the program.
The "
make" tab at the top of the screen, click on that and select "
compile".
When it has compiled you will notice a file called "
helloworld.exe" has appeared in the directory with the helloworld.bas file.
This is your compiled program, you can either run it by
double clicking on it or straight from fbedit by pressing
shift and f5It opens a screen, (I am not going to go into details of this command as we will be using a different method for screens later on)
Prints Hello World on the screen
Waits 3 seconds
Then exits
It may not seem like a lot, but there are some concepts to learn here, the programs execution starts at the
top line.
The top line is white space which is ignored, so are the next lines (which are comments), the
first command that is looked at is the screen command.
It opens the screen
the
next command it comes to is the print command.
It prints out our message on the screen.
Then it comes to the
sleep command.
It pauses for 3000 milliseconds (3 seconds)
then it encounters the
end command
The program is safely terminated.
The concept that you need to grasp from the outset is that of the list, this is why programs are called listings,
each item in the list is executed one after the other. just like going through any list and reading off each item one by one.
This is as simple as it gets, clearly a program is just a list of instructions, the computer has an internal marker to keep track of where it is in the list, the list contains commands for the computer to carry out, it executes an instruction, moves the marker down the list to the next command and executes that, repeating the process until the list is finished.
We'll look at two of the commands here.
Firstly the
print command.
Only briefly because this is going to be used to output the results of some stuff we do in a little while.
At its simplest, the
print command is followed either by a variable or some alphanumeric text (letters, numbers and symbols).
Our
"hello world
" message is surrounded in
quotes "" You'll notice that fbedit coloured them red, the message within was coloured black and this is what got printed to the screen. There is another variation, we'll come to that in the next tutorial, but putting it simply, the message to be printed is contained within a pair of these quotation marks.
We will use the print command in the next few tutorials, but we'll be moving on to better things as print is very limited really.
The next one is the sleep command.
It was necessary to specify a delay before the program ended otherwise it would have been all over before you had a chance to see it.
You type the command;
sleepfollowed by a delay in milliseconds
1000 milliseconds
= 1 secondSo.
Here's one last example, apologies that this tutorial is very basic(pun intended), we will do some more interesting stuff next time!
Your excercise is..
Open a new source file
type this listing;
' Program 02 "Hello World 2"
' Written by me on 17th July 2009.
' --------------------------------
'Open an 800 pixel wide 600 pixel high screen
Screen 19,,1,,
Dim As Integer a
'Print our message
Print "Hello World!"
'Wait 1 second (1000 milliseconds)
Sleep 1000
'Print our message
Print "This is my second attempt!"
'Wait 2 seconds (2000 milliseconds)
Sleep 2000
'Exit the program
End
See if you can work out what it will do before you run it.
Now modify it to add some more messages at the end.
See you in the next one!