Vol. 2 Iss. 4 - October 2001

The Very BASICs

By Sane <sane@telia.com>

This is a pretty old tutorial that I wrote about 2 years ago or something, and thanks to Wildcard, I came to think of that it could be used in QBCM, so now it's here, and I hope someone gets something useful out of it. Otherwise it does its job as a filler :)

A simple Hello World program...

As almost all other programming tutorials for beginners, this tutorial has also got a "Hello World" program.

The program is basically a program printing "Hello World" to the screen, and here it comes:

'A simple "hello world" program, made by Sane

PRINT "Hello World"

If you typed that text into QB, you have already made your first program!

As you can see, if you typed this program into QB, and started it using Shift+F5, the PRINT function in QB prints what is between the quotes.

If you use ' at a line, everything after that sign on the line will be ignored by QB, and thus you can make comments or remarks in your programs, to make understanding your own programs easier.

Now you may ask: "Why wouldn't I understand my own programs, I'm not that stupid", or something like that, but if you start getting used to making remarks and comments now when your a beginner at programming, you won't have to learn the hard way, when you make larger projects, why you should use comments. For example, when I started to make a big program about one and a half year ago, and I didn't program on it for about half a year, I couldn't understand what I had been doing in the program, how, or why, when I thought that I should start programming on it again, and thus I was forced to start making comments...

My suggestion to you is, as you may have understood, to always make comments in your programs, for your own sake, and to make extra empty lines where you see it needed to make the program code easier to read.

The function END is optional to use in cases as this one, but further on we may want to end the programs when a certain event occurs or so, and thus the END function is useful anyway.

Getting a bit more complicated...

'A program for getting and printing the users name to the screen
'Made by Sane

INPUT "What is your name"; Name$

PRINT "Your name is "; Name$; "!"


From now on, it's suggested to type and run every program shown in this tutorial, unless told otherwise.

After you have executed this program in QB (using Shift+F5, as always), return to this tutorial.

The program shown above introduces three new things:

  • the INPUT function
  • more on using PRINT
  • variables

    Here comes an explanation:

    INPUT is a function that prints the text within quotes, and lets the user type a line of text into a variable.

    The semicolon after the quotes in this example makes INPUT type a ? sign after the text within quotes.

    If you do not want a ? sign there, use a comma instead of a semicolon.

    Name$ is a variable, which means a place in the memory where information can be stored, that is called using a symbolic name, in this case Name$.

    The dollar-sign in Name$ means that the variable is of the type STRING, which is a text-variable type, and the only one QB uses.

    QB doesn't take any notice wether letters in variablenames are uppercase or lowercase letters, and thus Variable% refers to the same variable as VaRiAbLe%.

    What symbolic name the variable should have is in general up to you, but there are some rules for variable names:

  • The variable name may not start with a number.
  • Variable names can't use any characters but A-Z, a-z and 0-9 (except for dollar-signs for example, but dollar-signs and other "type signs" may only be used as last character in the variable)
  • Variable names may not use the same names as existing QB functions.
  • Variable names may not contain more than 40 characters.

    A variable uses the same name in the whole program, so if you want to reach the variable blah$, for example, you can't use the name blahahah$ instead.

    Variables can be of other types too, like integers, long integers and such, but more of that will be discussed later on.

    If you want to print a variable using the PRINT function, use for example PRINT Variable$

    If you want to use variables together with text, you may do it like this:

    PRINT "Text";Variable$

    When using PRINT, semicolon makes PRINT continue printing directly after the last text printed, and thus, if Variable$ would contain "blah" in the previous example, the text printed would be "Textblah". If you use a semicolon at the end of the PRINT line, the next PRINT call would print its text directly after the previous text printed. If you use a comma instead of semicolon when using print, there will be a TAB space between the text before and after the comma. If you don't understand what I mean, try it out yourself.

    This is all that should be needed for this section, I think, so let's move on to some more about variables...

    More about variables

    As mentioned earlier, there are other types of variables than string variables.

    If you have not declared that a variable should be of a certain type, either using for example $, or done it some other way, the variable is of the type SINGLE, which means that it's a "single-precision value" variable, which in turn means that the variable can contain decimal values, but not with as many decimals as "double-precision value" variables.

    The "type sign" for variables of the type SINGLE, is !, just as $ is the "type sign" for variables of the type STRING. INTEGER variables have the type sign %, LONG(long integer) variables have got the type sign &, and DOUBLE(double-precision) variables have got the type sign #.

    All value type of variables can be used in mathematic statements, here are some examples:


    Note that if you for example do as in the second example above(t%=t%+2), t% becomes what it is, plus 2, so if t% is equal to 4 before, t is equal to 6 after.

    String variables can also be used with the + sign, like this:

    S$="Hello"+" "+"World"

    Note that if you for example do as in the second example above(t%=t%+2), t% becomes what it is, plus 2, so if t% is equal to 4 before, t is equal to 6 after.

    Here comes a small table with the limits of the different variable types:

    Type: Min: Max:
    String ($) length 0 characters 32,767 characters
    Integers (%) -32,768 32,767
    Long Integers (&) -2,147,483,648 2,147,483,647
    Single precision numbers (!)
    Positive: 1.401298 E-45 3.402823 E+38
    Negative: -3.402823 E+38 -1.401298 E-45
    Double precision numbers (#)
    Positive: 4.940656458412465 D-324 1.797693134862315 D+308
    Negative: -1.797693134862315 D+308 -4.940656458412465 D-324

    At first, I was going to write some more boring variable stuff, but then I realized how boring it would be for me if I was a person reading this as a beginner programmer, so I think we'll leave the rest until it's needed later on...

    Labels, GOTO and GOSUB

    In QB there are functions for jumping between different places in your program, and this is what this chapter will be all about...

    The first function you'll learn how to use is GOTO.

    GOTO is used to jump to the row with the label you specify, here comes an example:

    PRINT "This program won't ever stop..."
    GOTO Label 

    If you type the program above into QB and run it, you'll notice that it prints "This program won't ever stop..." until you end the program, which is done using Ctrl+Break on the keyboard.

    The only label in this program is the label named Label, as you might have noticed...

    Labels are defined by putting a name at the beginning of a row, followed by a colon, or by using a number. When using a number only, you can choose by yourself wether to use a colon, or just a space.

    The rules you need to think about when making label names (except for when you use numbers only), are the same as for variable names.

    Now on to using GOSUB...

    GOSUB is a bit different from GOTO, in the way that GOSUB returns to the place where it was if you put RETURN where you want GOSUB to return, here comes an example:

    GOSUB PrintText
    PRINT "GOSUB finished"
    PRINT "This text is called using GOSUB"

    This program will call the text printing using GOSUB, and then print "GOSUB finished", and end after that.

    GOSUB is pretty useful for making sub-programs, and there is a better way to do that, but for now we'll stay with GOSUB.

    Well, seems as if this chapter is already completed... :)

    So you want to stop making linear programs, huh?

    Ok, we'll find something for ya... :)

    This chapters first command is IF, which is used for condition checking.

    And here comes the example, as usual...

    INPUT "Do you like this program?(Yes/No)", Answer$
    IF Answer$="No" THEN GOTO Start
    PRINT "Oh, so you like my program. Thank you."

    If you try running this program, then you might notice that it doesn't accept "No" as an answer (even though it accepts "no" or "NO"...).

    The IF statement in the program checks if Answer$ is equal to "No", and if it is, it jumps back to the start.

    Another new command for you is the CLS command, which CLears the Screen, and when I noticed that I had forgotten to write about good ol' CLS, i was pretty shocked :), but at least it's not still in the "land of forgottenness"... :)

    Back to the IF command, you see that IF is used like this:

    IF condition THEN statement

    In the condition part, you may use the following operators:

    Operator Means
    = Equals
    < Less than
    > Greater than
    =< Less than or equal
    => Greater than or equal
    <> Not equal

    Example of another way of using IF:

    'Made by Sane
    INPUT "How old are you?";age%
    IF age%<13 THEN
     PRINT "You're a child"
    ELSEIF age%>=13 AND age%<20
     PRINT "You're a teenager"
     PRINT "You're an adult"
    END IF

    As you saw when you executed this program, it asks for the user to give it his/her age, and using that, tells the user what he/she is: a child, teenager or adult (as if the user didn't know that already :)

    Some not-so-obvious info about the new things introduced in the example:

  • In IF commands, you might have use of either AND, OR or XOR. AND is for checking if both conditions of two conditions are true, OR checks if one/both of them are true, and XOR checks if one and no more are true.
  • It's possible to use as many ELSEIF:s as you want to.
  • You don't have to use ELSE/ELSEIF.
  • You always have to use END IF in an IF statement spanning over multiple rows.