BASIC Programming Basics
This is a brief introduction to programming, and especially BASIC programming. You should read the tutorial Getting started first as this shows you how to setup QB64 and how to write a simple program (which you will need to do in order to test the examples here)
In the Community of QB64 we are used to programming, so we might not always appreciate the difficulties of learning for those that are new to the concept. This Tutorial will try to explain to the best of my efforts basic concepts of programming and programming in BASIC (apologies if it seems to be written to a 3-year old because of this, but I want to cover as many obstacles as I can find and it's my impression that it is a good way to learn)
Programming is a way for you to tell the computer what to do. The computer is your "slave" and has to do what you command it to do. For the computer to know what to do you have to give it instructions in a way that the computer can understand.
ABOUT PROGRAMMING CODE:
Now, the instructions that the computer can understand are very complex and non-intuitive, so languages were developed to make instructions easier, in one BASIC instruction there can be many instructions accomplished that you don't need to worry about. Even Assembly is a set of instructions to make the computer instructions easier, but BASIC is much easier than assembly.
BASIC stands for Beginners All-purpose Symbolic Instruction Code.
So it's made for beginners, and you can use it for whatever need you have, hence all-purpouse, there are other languages that have beginners in mind, but one part of the philosophy of BASIC is that even though it is simple it can do many advanced tasks as well, to keep you inspired and to help you build the projects that you want to. Many languages buildt for beginners only makes you do certain tasks and limits your freedom. BASIC does just the opposite, it tries NOT to limit you, it trusts you to know that you shouldn't take the KILL instruction lightly (as it will erase files from the hard disk), and when you finally need the KILL instruction and have learned how to use it then you are free to do so!
ABOUT THE BASIC INSTRUCTIONS:
The BASIC instructions are often called "Statements", or "Commands", in the wiki and in many situations they can also be called "Keywords" so you might want to remember that as well, "statements" are used in this tutorial though from now on (and bolded so that it repeats in your head :) ).
The statements have names that resembles the english language, like FOR, NEXT, IF, THEN, GOTO (go to), etc., so when you have learned how they work they are very easy to remember. Some statements are shortened so that it is easier to type (like CLS - CLear Screen), but once you learned them you are instantly reminded of their use if you look at the name.
Read more about statements here!
Many instructions have arguments that lets you put information into the instruction.
Arguments are the information that you give to a statement in order for the statement to do as you want, a argument can be the text to display to the screen, or what color a dot on the screen should have. In real life you could give someone an instruction to paint your car, the argument would be which color the car should be - or even if it is the car that should be painted or the house (so it depends on the statement what arguments that goes with it).
There are also statements without arguments, and statements where some arguments are optional, you can read about that in the Argument page.
One important part of programming are variables, the name variables comes from the fact that they can change, a variable can hold a number for example, and then you can change the number to whatever you want. The variable can have any name you want except for BASIC statements (since the computer will think it is a statement and not a variable).
'x' can be a variable, now we can tell 'x' to be whatever number we want, if we want it to be '10' then we just type this:
x = 10
'x' equals 10, or if we type this:
x = 10 + 1
'x' equals 10 + 1, so now 'x' equals 11.
So we can tell 'x' to be any number we want. 'x' can of course be any name you want, you can for example name it 'money' instead, like:
money = 1000
Which makes 'money' equal 1000.
For variables to be useful we need something to display them, BASIC has a statement for this of course, and it is named 'PRINT', PRINT displays what you want to the screen. So we can type this:
money = 1000 PRINT money
Now the value that 'money' holds is displayed to the screen.
If you want PRINT to display text you need to have the text in quotation marks (") so that it isn't being confused with variables. So we can type this:
This will display the text "Hello" to the screen. You may want to change the color of the text, the COLOR statement does that, there are 16 colors to choose from (0 to 15) and each color is a number (4 is red for instance), so if we want the text to be red we type this:
Sometimes we would like to display both text and the value of a variable, then we have to seperate the text and the variable by a semicolon ";" like this:
money = 1000 PRINT "Money:"; money
This will display "Money: 1000" to the screen.
ABOUT TEXT VARIABLES:
In order to differ ordinary value variables from text variables we use a $ after the variable. Like values we can set a text variable to be whatever text we want, be sure to use quotation marks for the program to know it isn't a variable ("), like this:
myname$ = "Arnold"
You can also add text and display it, like this:
myname$ = "Arnold" + " Schwarzenegger" PRINT myname$
Of course you can also combine everything, like this (CLS clears the screen):
The text "Arnold Schwarzenegger" can of course be contained in a single text string, like this:
myname$ = "Arnold Schwarzenegger"
Text variables are often called "strings".
Sometimes we would want the user of our program to be able to set a variable by themself, this can be accomplished by using the statement INPUT, like this:
INPUT "Your name: "; myname$
This will display "Your name: " on the screen while waiting for the user to input their name. You can later display it on the screen using PRINT, like this:
COMPARING PROGRAM VALUES:
IF is a very important statement, it compares your variable to a certain condition and if that condition comes true then you can tell the computer what to do next.
For example, if the user has less than 10 dollars then you might want to reply that they are poor, and if the user has more than 1000 dollars then you might want to reply that they are rich, like this:
< is less than and > is more than. What if the user has between 10 and 1000 in money? Then we can do like this:
When using AND both conditions has to be true, money has to be more than 10 and money has to be less than 1000. But what if money equals 10 or 1000? Well, we could make a new condition like this:
When using OR either condition can be true and it will PRINT "You are neither rich nor poor.", however it isn't necessary to have a new condition, instead you could just do it like this:
>= is more than or equal to, <= is less than or equal to. Ok, so now we might want to remind them that they are broke if money equals 0, like this:
So, let's show the program in its full glory!
CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money PRINT "Your name is: "; myname$; " and you have"; money; " dollars!" IF money < 10 THEN PRINT "You are poor." IF money > 1000 THEN PRINT "You are rich." IF money >= 10 AND money <= 1000 THEN PRINT "You are neither rich nor poor." IF money = 0 THEN PRINT "You are broke!"
Ok, but there is one thing to be added, you see, if money = 0 then it will print both "You are poor." and "You are broke!"...we can make it only print "You are broke!" if money equals 0, we only need to change the first condition (IF money < 10) to this:
<> is "not", so if money is less than 10 and money is not 0 then it will print "You are poor.", so when money is 0 then it will only print "You are broke!".
Let's show the entire program one more time:
CLS INPUT "Your name: "; myname$ INPUT "How much money do you have?: "; money PRINT "Your name is: "; myname$; " and you have"; money; " dollars!" IF money < 10 AND money <> 0 THEN PRINT "You are poor (or you owe money)." IF money > 1000 THEN PRINT "You are rich." IF money >= 10 AND money <= 1000 THEN PRINT "You are neither rich nor poor." IF money = 0 THEN PRINT "You are broke!"
I also added "(or you owe money)" as the user might enter a negative value (one that is < 0 like -10) and this program won't cover that situation, to practice you could try to find a way to cover that.
Sooner or later you will want to play around with graphics. As you might have noticed all we have done in this tutorial as of yet is to discuss how to do things with text, you can display text in a graphics mode - but you can't display graphics in a textmode, this section will cover how to setup a graphics mode. Here follows some easy to use graphics statements:
- SCREEN - Sets a different screenmode so that you can use graphics.
- PSET - Pixel SET, which sets a pixel anywhere on the screen in any color.
- POINT - Which looks at a point on the screen and gives what color that point is.
- CIRCLE - Which draws a circle anywhere on the screen, in any size and any color.
- LINE - Which draws a line between two points on the screen, in any color.
We start with SCREEN, as we have to change the screenmode from text to graphics. There are many different screenmodes but we start with screenmode 13 (which is 320*200 and 256 colors). We simply type this:
Now we are in screenmode 13, which can be used to display graphics, you have to have that line in the program for any of the following statements to work.
A good place to start when displaying graphics is PSET, it draws a pixel on the screen (a pixel is a point on the screen that is lit and can be in any color), in order for the computer to know where to draw the pixel we need to give it coordinates, the x coordinate is the horizontal coordinate <------> and y is the vertical coordinate (up/down), so we can do like this:
PSET (50, 100)
Where 50 is the x-coordinate (left-to-right) and 100 is the y-coordinate (up-to-down). Notice that we didn't need any argument for color as it will just assume the current text color when there is no argument, so we might want to find out what color it is. We can see that it is white, but what value is that? Well, let's find out using POINT, it looks at a screen coordinate and gives what color it is, we let a variable named 'col' store the value (as we can't use 'color' since that is a statement):
And it prints 15 in bright white! We might want to change color to...let's say red. I already know that the value for red is 4, so we can do like this:
PSET (50, 100), 4
The third argument is color. So now the pixel will display in red instead of white. We can point it to see the value:
And it should show 4 (in bright white, as setting the color of the pixel doesn't change the text color, the COLOR statement changes text color).
That should cover PSET, but we have the ability to easily draw a circle on the screen too using the CIRCLE statement, it also deals with x- and y-coordinates, like PSET, but must also have a radius (how big it is from the center), like this:
CIRCLE (50, 100), 10
This makes a circle at position 50, 100 and with a radius of 10 pixels (with the current text color as we don't have any color argument), to make the circle red (color 4) we simply add the color argument:
CIRCLE (50, 100), 10, 4
That's the circle, there are more arguments, but since this is a beginners tutorial we don't want to complicate things, if you are curious of the other arguments then you can click the link of the statement and read about them there.
So what about lines? Well, the LINE statement can make both lines and boxes, I'll show you how, first the line:
LINE (50, 100)-(200, 150)
This makes a line from position 50, 100 to position 200, 150 (in the current text color). Then we can make it red (color 4):
LINE (50, 100)-(200, 150), 4
Now we can make it a box:
LINE (50, 100)-(200, 150), 4, B
B stands for Box, if you don't want the color argument you must do like this...
LINE (50, 100)-(200, 150),,B
...to show that there is no color argument (which makes it white if we haven't changed the text color), you can't put 0 there as 0 is black. If we want a filled box then we would simply do like this:
LINE (50, 100)-(200, 150), 4, BF
I made it red again too, since I like red...BF stands for Box Filled.
NOTE: DECLARE SUB or FUNCTION is ignored by QB64. It is used in QBasic to tell it the SUB name and parameter types.
DECLARE SUB MySub(number AS INTEGER)
QB64 doesn't need to know that information and will ignore DECLARE SUB or FUNCTION statements!
We'll start with the SUBs:
A SUB is a part of a program that you can CALL and the program will jump to the sub and follow the instructions that is contained within the sub. A sub has to end with END SUB as the program has to know when to jump back to where you were when you called it.
Subs must be after all the other code.
You can do this for example;
There is no limit in QB64 for how large a SUB may be, you can write a entire program within a sub, but nothing will happen unless you CALL it!
As you can see if you click the link to SUB there is much more information to learn about subs, but this will get you started.
As with subs, functions must also be after the other code. Since functions return a value we need to give that value to something, so we'll start by giving it to a variable. We'll call it 'ret' which is short for 'return', we can't use 'return' since that is a reserved word (see: RETURN).
Ok, so ret is 0 as we can see by PRINTing it. This is because we haven't told the function what to return (and the default is 0 if it is a numerical function or a empty string if it is a string function).
Let's make it return something else, you do this by assigning the function the value you want returned, it might sound complicated but it isn't. This is what you should do to give the function a value to return:
So when we run this code ret is 5 since the function returned 5.
This isn't very useful in its current state, we would like to give values to the function so it can return something based on the values we give. That brings us to arguments which can be used to give subs and functions values to use within the sub or function.
Whether it is a sub or a function arguments are enclosed in paranteses with a comma between each argument. Like this;
FUNCTION (argument1, argument2, argument3)
Any number of arguments needed can be used.
Here is a example using arguments in a function:
It prints 30, as 10 + 20 is 30. We can give the returned value directly to PRINT if we want, as PRINT accepts returned values (the function acts like a argument). For example;
The same code, but we don't give it to the variable 'ret' but directly to PRINT.
This is a example using arguments in a sub:
You don't actually need CALL, just using the sub name will do, like this;
The only difference is that I removed CALL which isn't really needed but can make the code more clear. Also, notice that when I removed CALL I removed the paranteses as well, they are only needed when using CALL to call the sub and will cause a error if you use them without CALL.
As you can see using arguments is rather easy, and subs and functions can save you having to do things over and over!
That marks the end of this tutorial!
Note: This tutorial has only scratched the surface of the statements and concepts used. It is meant to get you on the right track, as such many other features of the statements can be learned, at your own pace by following the link to the explanations of the statements. If you are still unsure, stop by this place from time to time as I will add more information for beginners, and if you are unsure about something don't hesitate to stop by at the forums, we (the Community) at qb64.org would be glad to help you!