UCS: The Ultimate Computer Source
A Thinkquest 1999 Entry

Front | Teach | Thinkquest | About UCS

[ History - How? - Internet - Programming - Glossary - Issues - Operating Systems ]

Click here for graphical version

QBasic Tutorial

  • Explanation
  • Compilers and Interpreters
  • Simple Commands
  • Dim, Variables, Loops
  • Advanced print, Using Variables, If/Then/Else, Input, Sound
  • Chapters Review
  • Arrays
  • Math
  • Screen Modes
  • Graphics
  • Goto
  • Chapters Review
  • File Access
  • Tips
  • Tutorial Review
  • Keyword Reference


    QBasic Tutorial - Chapter 1
    Explanation

    Oh no! It's... a HISTORY LESSON!!!

    BASIC stands for Beginners All Purpose Standard Instruction Code language. It was one of the first easy-to-learn language and was developed to be a beginners programming language. It was originally designed as a mainframe timesharing programming language in 1963 but it was later used as a standard programming language. Many flavers of Basic came out, such as Applesoft Basic, QuickBasic, and (barely like Basic) Visual Basic.

    QBasic is a modification of Basic written by Microsoft that handles things very slightly differently. This tutorial will concentrate on QBasic

    QBasic Tutorial - Chapter 2
    Compilers and Interpreters - How to Write in QBasic

    In this chapter you will find out how to compile your programs or run them. You will find that it is actually very easy and quick to make a program in QBasic. For the rest of these chapters you will need a QBasic compiler or interpreter.

    The difference between a compiler and interpreter is that a compiler actually makes a program that you can execute. An interpreter just makes the program executable from the interpreter itself... it does not compile your code into a stand alone program.

    Most MS-DOS distributions feature a program called qbasic.exe which is a full functioning QBasic interpreter, by Microsoft. It's in the \dos directory.

    QBasic Tutorial - Chapter 3
    Simple Commands - Print, End, Cls, Rem

    In this chapter you will learn simple commands. These commands make the actual program.

    Here is a simple program that will be explained:

    Example:
    REM My first ever program.
    CLS
    print 'Hello, this is my first program.'
    end

    Explanation:

    Rem - This command makes a comment in the program. It does not do anything at all for the actual program, it's just for convenience. When you get to more advanced programming, you will often needs reminders to tell you how you are planning on doing certain commands, because things can get very confusing when you have many lines. NOTE: REM can also be replaced with a ', so:

    ' My first ever program.

    ... would do the same thing.

    Cls - This command 'cls' clears the screen of all text and video. Most programs should start with this.

    print 'Hello.... etc' - This command 'print' is the command you will use the most in QBasic. Print sends a line to the user, following this format: print 'text.'

    end - 'End' ends the program. If you don't end it you will get stuck in the program. Consult your QBasic program for the key to exit your program if you get stuck. Sometimes, you can exit out of your program by hitting CONTROL-C.

    QBasic Tutorial - Chapter 4
    Dim, Variables and Loops

    Variables and loops are very important to any program, as they make things easier to do and can make things more compact and easier to remember.

    A variable is a keyword assigned to a piece of data to make it easier to remember and access. There are a few types of variables:

    String

    $ String - "Hi!"
    % Integer - 2
    & Long - 28339
    ! Single 20.3993
    # Double - 991029.91

    Each is represented by a symbol, which can be used to declare it. There are a few ways to declare a variable:

    DIM varname as type

    (Example: DIM myname as string)

    or

    DIM varname(symbol)

    (Example: DIM myname$)

    To just declare a variable and let the interpreter decide what the variables type is, just leave out DIM and do:

    variable = value

    (Example: myname = "Pete" or myage = 14)

    Or you could do:

    varname(symbol) = value

    (Example: theyear% = 1999)

    ... and just leave out the DIM. It's good practice to use DIM anyway, it will prepare you for arrays later on.

    A constant is something that will be explained briefly. Basically, a constant is a variable that stays the same. Just do:

    constantname(symbol) = value

    Common example:

    pi! = 3.1415

    A loop is something that repeats a set of instructions, or a block of code, as many times as it is told to.

    A for loop does something for a certain amount of times.

    Example program:
    Cls
    print 'This is the Chapter 4 Program'
    examplevar=5

    for i = 0 to 20 do
    print "This will repeat 20 times!"
    next i
    print "This isn't in the loop!"
    end

    Explanation:

    examplevar = 5 - This means that you are declaring a sample variable called "examplevar," and it equals 5.

    For i = 0 to 20 do - This will execute any following code 20 times. If the 20 were a "50," it would execute 50 times. I is used as a temporary variable. That's just how the format is.

    next i - This tells the program to run the loop again unless i is passed 20, or what the loop is set to. I.e., the loop runs 20 times until i = 20, then the loop terminates and the program continues after i = 0.

    print "This isn't in the loop!" - This print statement isn't in the loop because it's outside of next i, which marks the end of the block of instructions that are to be executed multiple times by the loop.

    A while loop does something until a certain condition becomes FALSE. It uses the same format (in a way) as IF so if you're confused, after you read the next chapter, come back here and read this part again.

    Example:

    while a = 5
    print a
    a = a + 1
    loop

    (This would print: 1 2 3 4 5).

    The loop goes on until the statement given after while becomes false. The thing is, unless the variable included in that statement is modified, the while loop goes on forever. So, it is very important that you make sure that your while loop has a way of ending.

    QBasic Tutorial - Chapter 5
    Advanced print, Using Variables, If/Then/Else, Input, Sound

    In this chapter you will learn commands that will allow you to make very powerful programs, with sound, questions, and interactivity.

    In this chapter the term advanced print refers to putting variables inside a print statement. For example, you could use the input statement to ask a user for his or her name, and then have the program return "Hello (Persons name!)" which would add greatly to the programs interactivity, plus it's kind of cool.

    Explanation of a few commands:
    Now that you are getting to more advanced things in QBasic, the commands to be used needs to be explained before so it isn't entirely confusing.

    printing variables: print "Any text here";varname" any other text."

    sound: sound <tone> <duration>

    input: input "Hello, what is you're name?"targetvarname;

    if/then/else: If then is used with number variables and not as often, text variables like "Pete" for example. You'll see an example of how to use an if then statement in the following program.

    Example:
    REM Programming with input sound and others
    CLS
    sound 40,60
    print "This program will involve some of the things we have talked about so far."
    input "What is your name?";name
    input "How old are you?"; age
    if age > 200 then print "You are old!":print "Hahahaha" else print "Cool."
    print "Hello, ";name;"!"

    Explanation:
    REM - Entered a comment
    CLS - Clear the screen

    Sound - Made a short beep (NOTE: for different beeps, experiment with the tone)

    Print - Prints the message

    Input "What is your name?"; name; - Asked the user for his name, and stores in a string variable.

    Print "Hello,";name;"!" - This line included the variable that was stored before, "name."

    input "What is your age?";age; - This stores the users age (to be inputted) into a number variable (integer) called "age"

    if age > 50 then print "Haha! You are old!":print "Hahahaha" else print "Great!" - This statement is long and needs to be explained more:

    if age > 50 is telling the program, "If the variable age (that was entered by the user) is more than 50 then....."

    print "Haha... etc":print "Hahahaha" - If the previous condition was met before (age is more than 50) then print "Haha! You are old!"

    The :print "Hahahaha" means to also print "Hahahaha." You may be wondering, what is the : for? The problem is, after command in an if statement (print "Haha! You are old!") the program immediately looks for an "Else" statement, otherwise goes on with the next state- ment in the code. The : means to do another command. You can do this anywhere in the code, but it is usually meant to fit more than one statement in a "then" statement.
    i.e.:
    If testvar > 40 then print "1":Print "2":print"3":print "Still going!" :print "This can go on forever."
    Else print "Now it's time for the Chapters Review!"

    CHAPTERS REVIEW (1-5)

    In these chapters you learned how to make a very simple program, involving loops, sounds, commands, text, and you also added interactivity, by asking the user for his/her name or other information. In the next 5 chapters you will learn more advanced programming, including graphics, screen modes, adding subtracting multiplying and dividing variables with math, arrays and more!

    Review keywords

    You learned the following keywords and their functions in the chapters before:

    CLS - Clear the screen : Cls

    PRINT - print a statement : PRINT "Hello!"

    INPUT - Asks user something to be stored in a variable - INPUT "Age?" age;

    SOUND - Makes a beep for desired tone and duration - SOUND 50,30

    LOOP - Executes a block of code desired number of times. :

    for i = 0 to 20 do
    print "This will repeat 20 times!"
    next i

    END - Ends the program

    And If/Then/Else statements - Look back at chapter 5

    QBasic Tutorial - Chapter 6
    Arrays

    An array is a feature in QBasic, and many other programming languages. Think of one as a file cabinet. It can store information, and that information can be called undere that one single cabinet, making it easier and more convenient, and makes your code look a lot more organized.

    The DIM command is used to declare a variable. In the QBasic language you will often see it. In QBasic you have seen that we totally skip the DIM command. Just for a little practice, you will use the DIM command. If you want, prefixing your variables with DIM will work just as well.

    To make an array, we use the DIM command.

    DIM is used to create how big the array should be, and entries in the array are defined just like a variable is. Each entry IS a variable after all.
    CLS
    REM Arrays
    DIM a$(5)
    a$(1)="Entry 1"
    a$(2)="Entry 2"
    a$(3)="Entry 3"
    a$(4)="Entry 4"
    a$(5)="Entry 5"
    print "The third entry is: "; a$(3) ;"."
    END

    As you can see, it its format for being displayed inside a print statement is the same as using a variable.

    Arrays can have more than one dimension.

    DIM $a(5, 5)

    This would make an array of organized variables such as:

    $a(1,1) $a(2,1) $a(3,1) $a(4,1) $a(5,1)
    $a(1,2) $a(2,2) $a(3,2) $a(4,2) $a(5,2)
    $a(1,3) $a(2,3) $a(3,3) $a(4,3) $a(5,3)
    $a(1,4) $a(2,4) $a(3,4) $a(4,4) $a(5,4)
    $a(1,5) $a(2,5) $a(3,5) $a(4,5) $a(5,5)

    That's all an array is, basically; an organized shortcut to variables that can be changed in an organized way, like spreadsheet cells (sort of).

    CLS - Clear the screen

    REM - Comment

    DIM a$(5) - Make an array called a that can hold 5 pieces of information. They are:

    a$(1)
    a$(2)
    a$(3)
    a$(4)
    a$(5)

    They are just like string variables, and you set them the same way, and call to them the same way as variables are in PRINT.

    print "The third entry is: "; a$(3) ;"." - You hopefully should understand by now.

    The advantage of arrays is you can easily organize your information into neat little arrays, and someone trying to read your code would understand it a lot better.

    QBasic Tutorial - Chapter 7
    Math

    As you advance into advanced programming in Basic, you will find that adding variables, subtracting them, or changing variables will be crucial for a successfully interactive program.

    Another great thing about using such math is help with homework. If you are just clumsy with basic math, you could take 3 minutes, make a small program which asks you for the required numbers, and have great help with your homework!

    Math in QBasic is almost like typing into a calculator, only a little different. You can add a number to a variable (assuming it's an integer) by using <variable> = <variable> + 5. That statement would add 5 to a variable by saying that the variable equals itself, PLUS 5. You can divide a variable by typing <variable> = <variable/5> and the same goes for subtracting, multiplying. The symbols for math are: + (Plus), - (Minus), * (Multiply), / (Subtract), ^ (To the Power Of)

    You can add one variable to another. For instance, <variable1> = <variable1> + <variable2>. Note that in that equation variable1 would be replaced by variable1+variable2. If you want variable1 to stay the same, you could do total=<variable1>+<variable2>

    Example program:

    REM My math program
    CLS
    input "Variable 1?" var1;
    input "Variable 2?" var2;
    total=var1+var2
    print "The total is (drum roll)...";total;"
    END

    Explanation

    REM - Enters a comment. NOTE - It's always a good idea to put your name in a program so if one day you decide to distribute your program and someone decides to use your program.... Your famous!

    input lines - The input statements asked the user for 2 different numbers : var1 and var2

    total = var1 + var2 - This statement adds var1 and var2 and puts them in the variable "total"

    END - Programs done!
    The print statement displayed the total to the user.
    Here would be how the program would look at the end (Example).

    Variable1? 5000
    Variable2? 5
    The total is (drum roll)... 5005!

    QBasic Tutorial - Chapter 8

    Screen Modes

    Before you get into graphics, you need to learn to to set up the screen to handle graphics. Don't worry - it's not even close to as hard as you'd think. In fact, controlling screen modes can be done with one simple keyword. Screen. The reason we must use the screen command is because when you initially start QBasic, the screen mode is set up for text, and it handles graphics very very very poorly. Setting the screen mode is as simple as typing 2 words in the beginning of your program.
    There are about 12 screen mode. You can experiment with what screen mode works the best for you.

    Here's the syntax (Syntax means the format of commands)

    Rem 'My Screen Mode Program' CLS Screen 9 print "The end." END

    What you'll see is that you CAN use the print (text) in graphics mode. In later chapters, you'll figure out how you can change the position of the imaginary cursor on the screen so that you can position text. You will also learn graphics, how to use them, and how to place them on the screen in different positions.

    QBasic Tutorial - Chapter 9
    Graphics

    Graphics allow you to make more interactive and eye pleasing progams. Before you start graphics you must set the screen mode, as you learned in the last chapter. We'll use screen 9 for now. You should experiment to see which graphics mode best fits your hardware needs.
    There are several ways of drawing to the screen. We'll concentrate on basic, simple methods of drawing to thee screen in QBasic. In the end of this chapter you'll learn more advanced graphics. Now, for the simple commands. Each uses things called coordinates to define what area of the screen to use. A coordinate is a specific pixel on the screen. It can be determined by counting the numbers of pixels down and the number of pixels to the right the pixel you want is. This method uses x, and y. X is down, Y is across. 50,50 would be 50 pixels down, and 50 pixels to the right.
    The first keyword we'll use is called pset, and it basically draws a single pixel on the screen.

    REM Graphics program
    CLS
    SCREEN 9
    PSET (10, 15), 7
    END

    PSET uses the format pset (#, #), #

    But, pset can be used like this: pset(#, #), but you have to set the color before that by using the COLOR keyword (color 7 would be the thing to use in this case).

    The next keyword you'll learn is called LINE. Line makes a line from one pixel to another.

    The format for line is LINE (#,#) - (#,#) #
    or like for PSET
    COLOR #
    LINE (#,#) - (#,#)

    The # represent pixels.

    Line can also be used to create a box, or a filled box.
    CLS
    SCREEN 9
    LINE (300, 100) - (400, 200), 1, B
    END

    The LINE keyword uses the same format, only theres a , B at the end. A box is created by using the two ends of the line (you should make them diagonal) as the top left or bottom right corner, or the top right and bottom left, depending on which way the line is going. B would make a hollow box. If you add F after B (BF) it will fill the box with the current color.

    The CIRCLE command is the easiest of them all. It works like PSET.

    CLS SCREEN 9 CIRCLE (100, 200), 50 END

    100,200 is the place where the center of the circle is placed. 50 is how many pixels out the circle should be.

    Now for some more advanced topics.

    Line, Pset, Circle, Square, are all nice commands, but unfortunately they are just simple implementations of the graphic ability of QBasic. Now for the next command: Draw

    Draw is a different way to quickly change colors and draw shapes without plotting pixels which can be confusing, time confusing, and inefficient.

    Imagine a robot. The robot can move up, down, left, or right. It can change colors. It can fill in spaces. It has some special things that can make it very convenient as well. It works like so:

    draw "drawstring"

    What makes draw (slightly) confusing is that it has its own commands that you should probably memorize.

    There will be a list of commands for the drawstring, but for now just check out the example:

    DRAW "c15 bm100,400 l5e5f5l5"

    c15 - set the color to 15 (white)

    bm100,400 - Move the imaginary pixel cursor to the plotted place: 100,400 without drawing.

    Lets break up l5e5f5l5:

    l5 - Left 5 pixels
    e5 - Up and Right 5 pixels
    f5 - Down and Right 5 pixels
    l5 - Left 5 pixels

    This makes a (drumroll...) triangle! But it was SO much easier than using LINE, or even (gasp) PSET.

    Now to learn about the possibilities of a draw string:

    Commands are seperated by spaces.

    C# - Set a color (Example: C15 (white))

    B# - Turn drawing off, like if you wanted to jump to a certain pixel using M. Drawing remains off until it's turned on again.

    M#,# - Move imaginary cursor to a specifix pixel plot (Example M50,50). If one of the #'s is prefixed with a - or + it's position moves relative to the number it's at, ie M-50+50 would move it down and right by 50.

    N - Returns the cursor to the position it started at after something is drawn.

    H# - Up and Left (Example: H5)
    U# - Up (Example: U5)
    E# - Up and Right (Example: E5)
    L# - Left (Example: L5)
    R# - Right (Example: R5)
    G# - Down and Left (Example: G5)
    D# - Down (Example: D5)
    F# - Down and Right (Example: F5)

    QBasic Tutorial - Chapter 10
    Goto

    Goto allows you to skip to a part of a code.

    gotokeyword:

    This marks the goto point. Then to skip back or foward to the point, you would type

    GOTO gotokeyword:

    Example:

    CLS
    i% = 0
    startit:
    print "Hi, you'll see this twice!"
    i = i + 1; if i = 2 then end else goto STARTIT

    As you can see here, this program used a technique involving GOTO to enable a loop to occur. The variable starts at 0. In the goto mark "startit" a message is printed, and the i variable is raised by one. Here's where the loop occurs: If i is 2, then quit the program, otherwise "startit" again. This loop can only occur once, because after the i variable is raised, it is at it's maximum to fulfill the loop condition (if i = 2 else)

    BASIC Tutorial - Chapters Review

    In Chapters 6-10 you've learned a LOT of advanced programming. You learned a lot of graphics, math, arrays, and goto, and it pro- bably seems very confusing. Don't worry. If you don't understand, try re-reading the chapter. If you are still stuck, you might even want to try reading the built in help in BASIC, but it's not very 'user friendly' in the wording of its help.

    In the next chapters you'll learn just as advanced programming, including file input/output (saving/loading things).

    If your compiler/interpreter uses line numbers, you can also GOTO a line number instead of defining a GOTO mark (like startit: ).

    BASIC Tutorial - Chapter 11

    File Access

    If you want to make programs that read records or maybe a scoreboard for a game, you'll have to learn how to save things to files, and read stuff from files.

    To open a command for access, we use the OPEN command. Open accepts some arguements, including what kind of access it should use. It can use:

    Input - Read Data
    Output - Write Data
    Append - Add Data to the End Without Overwriting
    Binary - Read or Write in Binary Mode
    Random - Used for Certain Types of Databases

    The format for OPEN is:

    open "filename.txt" for INPUT as #1

    The filename can be anything. #1 (could be #2, #3, #33, #50, etc) is used to refer to the file for later use.

    To write to a file:

    write #1, "Hi!"

    to read, you use a while loop:

    while not eof(1) 'EOF means end of file'
    input #1, varname$

    Here's an example:

    cls
    open "lala.dat" for OUTPUT as #1
    write #1, "Line 1"
    print "Info saved."
    end

    Then maybe anothe rprogram

    cls
    open "lala.dat" for INPUT as #1
    while not eof(1)
    input #1, line$
    loop
    print line$
    end

    The difference between append and output is that if you choose output, it erases the contents of the file when you write something. Append just adds text to the end.

    QBasic Tutorial - Chapter 12

    Final Tips

    You're nearing the end. Before the final message and review, you'll learn some neat things to incorporate into your program. Here are a few tips to use:
    1. Use/experiment with the sound system, you could make a soundtrack for your game!

    2. There is a variable called INKEY$ that, if a key pressed, the key value is stored (temporarily) in the variable. So you can use a WHILE loop or some kind of loop to read in key presses.

    3. QBasic programs are great for distributing because its simply a matter of cut and paste to use someones program. Find a program you like, and maybe experiment with that, but make sure to follow copyright laws.

    4. QBasic cannot compile .bas files. Only QuickBasic 4.5 can actually compile a .bas file into an executable. Of course, there are other programs that can compile .bas files that are free, but when it comes to Microsoft, QuickBasic 4.5 is the only one that compiles into an executable (program) form.

    QBasic Tutorial - Tutorial Review

    Well, you're done. Congratulations. The following last review chapter is to review what you've learned, and what to do after this tutorial.

    In 1-5 you learned how to make a basic QBasic program. Starting with a simple print statement, you moved onto a totally interactive program, with sound, computer input, variables, and loops.

    In 6-10, you learned how to do advanced math, setting screen modes, arrays, graphics, and other stuff.

    In 11-13, you learned how to use file access, and some tips and tricks you can use in your programs.

    After this tutorial, you should be able to make any kind of program that you want. Make games, make educational software, make a silly progam for fun, even make a program to do your homework!

    QBasic Tutorial - Keyword Reference

    Here is a list of keywords we've used in this tutorial for reference. If you have any questions about them, just skip back to the chapter that contains the keyword.

    Chapter 3
    CLS
    REM
    PRINT
    END

    Chapter 4
    DIM
    FOR
    DO
    NEXT
    WHILE

    Chapter 5
    IF
    THEN
    ELSE
    INPUT
    SOUND

    Chapter 8
    SCREEN

    Chapter 9
    PSET
    LINE
    SQUARE
    CIRCLE
    DRAW

    Chapter 10
    GOTO

    Chapter 11
    OPEN
    OUTPUT
    WRITE


    This concludes the QBasic tutorial.