Qbasic Tutorial : Learn Qbasic in a fast, simple, short, and intuitive way. Honest Effort Required.

--- Qbasic ---

CHAPTER 1


When I learned mathematics at school, I have to use "Tables", and learn how to do multiplication etc. with logarithm. Now, even students in primary school have computers.

About 25 years ago, an IBM 370 computer cost about 7 million Hong Kong dollars. Its main memory was only 256K bytes. Later mini-computers appeared. But still the price was formidable : 256K bytes memory, 8 inches floppy, 20 Mbytes hard disk .. cost roughly half a million H.K.dollar. At that time, hard disk of 5 ~ 10 Mbytes has a diameter of more than a feet, and weighed several pounds!

Now we can buy a PC with 64 Megabytes RAM, 15 Gigabytes hard disk, 15 inch monitor, floppy, CDROM, printer, scanner for less than H.K.$ 10000! (Hence it is good news for manufacturers who would like to computerize their operations!)

Many people shy away from programming, thinking it to be difficult. And manufacturers would mostly hire others to do the programming for them. Is it really that difficult to learn programming?

Several years ago, a Form 3 student visited me. At that time, that was a Wang Computer in my work place. That Wang computer can run BASIC, and was equipped with a cassette drive and printer. I thought it good for him to learn a little about computer. So I said to him, "Computer is really dumb, it understands very few words ! " Then I proceeded to teach him to write simple program, using "input, simple assignment statement, print, goto ", and showed him how to use "lprint .. " to print onto the printer. I only taught him for about half an hour. But he was hooked! He felt fascinated and came to my office repeatedly to write programs, he also read manual by his own . Later, he bought an Apple II computer, and even learned 6502 assembler programming! The time I spent with him was only about half an hour, and he can learn programming in that short time!

If you are willing, you can learn programming in a very short time too. (The lessons I prepared for Qbasic consist of only 12 chapters). Qbasic (a very popular programming language) comes free with DOS. Moreover, the PC you buy now is much more powerful than the 7 million IBM 370 25 years ago. (If you plan computerization many years ago but was hindered by lack of money, you wouldn't have such problems now.)

Microsoft did a very good job in improving Qbasic, and you can program practically anything, whether it be data-processing, or scientific number crunching, with it. (except for speed, the Qbasic Microsoft gives us free is an interpreter, though Microsoft has a Qbasic compiler). It is much user-friendly than many computer languages, e.g. COBOL, FORTRAN, PASCAL, C ....

After learning Qbasic, which should suffice 95% of your computer needs, if you are still interested, you may buy LINUX, a UNIX clone, for about US$ 50 ~ 60. (I bought a computer book from Taiwan, which included a Linux CDROM, for about HK$120.) LINUX comes with FORTRAN, PASCAL, C, BASIC (the original version of BASIC, not Microsoft's Qbasic), Bison (a compiler that can write other compilers) ,.... You would wonder why it is so cheap. It is because of the Finnish student Linus Torvald, he wrote the kernel of this operating system as a project under his teacher, and he put it on the Internet, free for everybody to download. Moreover, "Free Software Foundation" supply the operating system with many, many, very good quality software, free.

I wish to teach Qbasic in these 12 lessons. After learning Qbasic, you should be able to handle 95% of your programming needs, whether it be for data processing or engineering calculations. Qbasic should give you a good understanding of what a computer does, and after that, if you so desire, you may learn FORTRAN, C, ... (but these languages are really not necessary unless you want to enter this profession). Qbasic also gives you a good start in mastering Microsoft's Word, Excel, Powerpoint, ... or other companys' softwares, because you know programming, and you can learn such softwares much faster than one who doesn't know programming.

Programming also enables you to understand many aspects of the present Information Technology and will prepare you for technologies to come.

Before starting the lesson, it is advisable to get your son, or daughter, or anyone who knows computer to sit beside you.

(Note : my copy of Qbasic was stored on WINDOW 95 CDROM, under the directory D:\other\oldmsdos. There are 4 files in that directory, qbasic.exe, qbasic.hlp, help.com, help.hlp. I copied them onto C: , (you may ask your friend to do that for you if you don't know how to do that). If you have old computers with MSDOS, you can usually find Qbasic.exe, qbasic.hlp, help.com, help.hlp there.)

Although Window 95/98 replaces DOS, but Microsoft still retains a stripped down version of DOS with Window. Hence you have two operating systems in your PC.

To switch from Window to DOS :

Start -> programs -> MS-DOS prompt
After that, you may type in commands for DOS.
To switch back from DOS to Window, you type
exit


Exercise Calculate area of triangle

First switch to DOS operating system,

Start -> programs -> MS-DOS prompt
then type the command "Qbasic". Then you will be presented with an Qbasic welcome screen. You may follow that screen to read the on-line manual, or you press "ESC" to skip over that. Now type in (or download program)
   L20:
         input "Please enter height of triangle  "; height
         input "Please enter length of base  "; length
         area = .5 * height * length
         print "Area of triangle is = "; area
         goto L20

Computer really knows very few words, "input, print, goto" are keywords that must be spelled correctly (Qbasic will change all keywords from lower case to Capital letters, input will become INPUT, print will become PRINT, goto will become GOTO, ..).

If you made mistakes in typing, use "Delete" key and "Backspace" key (the one with "<-"). After checking for spelling, you may start to run the program.

To run a program, press "Shift + F5", now it will appear on the screen,

        Please enter height of triangle ?
Then you type in a number, say 4.7 and press "Enter".
        Please enter height of triangle ? 4.7
        Please enter length of base ?
You type in another number, say 5.23 "Enter"
        Please enter height of triangle ? 4.7
        Please enter length of base ? 5.23
        Area of triangle is = 12.2905
        Please enter height of triangle ?
Now you may enter another set of data. When you want to stop,

Press Ctrl + Pause ( = Ctrl + Break )
You may now save the program onto a floppy. Put in a new floppy.
Press "Alt", then "File " in the upper row "File Edit View Search Run Debug Options" will be highlighted. Choose "save", and you will asked the filename. Type in "a:\anyname" , (notice that the filename begins with a:\, this means floppy drive will be used. Microsoft uses a:, b:, c:, d:, e:, ... to denote disk drives. [ a:\, b:\ = floppies, c:\ = hard drive, d:\ = may be hard drive or CDROM ] If you forget a:\, microsoft will assume that c:\ , and your file will be stored onto the hard drive instead of your floppy.

Next time when you wish to recall the file,
Alt -> File > Old -> then type in filename, a:\anyname
And the program will be loaded back from floppy onto memory.

Exercise Calculate area of circle.

Press "Alt -> File -> New " then the previous program will be erased, and you may enter the new program.

Ans :

 
     L20:
             input "Please enter radius of circle ";r
             a= 3.1416 * r * r
             print "Area of circle = ";a
             goto L20

(Download program)

Exercise Calculate area of trapezoid

Ans :

         L20:
             input "Please enter length at top "; t
             input "Please enter length at bottom "; b
             input "Please enter height "; h
             ans = (t + b) * h / 2
             print "Area of trapezoid = ";ans
             goto L20

(Download program)


Note :

  1. Frequently used keys :

    1. ESC = escape = tell the computer to escape from the present task. It is very frequently used, especially when we have pressed wrong keys and wrong menu pops up.

    2. Tab = jump to the next field. Very often, we are presented with a "form" or "dialog box" with many fields to fill in. Pressing "Tab" will allow us to jump to the next field.

    3. Shift + F5 = tell the computer to start running program. Do not use "F5" which tells the computer to "continue to run a previously stopped program".

    4. F4 . Qbasic has 3 windows, (i) Edit window, where we type in programs, (ii) Immediate execution window, which is the window below the Edit window, separated by a horizontal rule, (iii) Output window, where Qbasic prints the output of your program.

      Pressing "F4" will toggle between "Output Window" and "Edit Window".

    5. F6 = toggle between "Edit Window" and "Immediate Execuation window".

    6. Shift + F1 = open the online manual.

    7. Place the cursor below a Qbasic keyword, e.g. "print, input, goto ,... ", and press "F1", then documentation about that keyword will appear.

    8. F2 . This will not be used at present. Later, when we write complicated program that comprises several subroutines, we will need this. Qbasic opens a window for EACH subroutine, as well as a window for the main program. "F2" is used to change to these windows.

    9. CTRL + pause ( = Ctrl + break ). This is used to stop program.

  2. We may use variable names like "height, length, area, r, a, t, b, ans, ...." as long as they are not Qbasic keywords. Very often, we would use a keyword without knowing it. Then either the computer will fail to respond as we expect it would, or that variable name would be changed into all capital. Then it is time to consult online manual (Shift + F1).

    Notice that we have used
    variable_name = arithmetic_expression
    This is called "assignment statement", the arithmetic_expression on the right will be evaluated, and the result put into variable_name. Hence you may find statement like
    a = a + 1
    which means "a is to be incremented by 1, and the result put back into a".

    Other examples are, e.g.
    
                   k = 3
                   k= k+ 7
                   print "The final value of k is = ";k
        
    k will be 3 at first, then k = 3 + 7. Hence the computer output will be " The final value of k is = 10 "

  3. The arithmetic operators are :

    1. + - * / for addition, subtraction, multiplication and division.

    2. ^ power, e.g. 2^3 = 8, a^(1/2) = square root of a.

    3. sin( ), cos( ), tan( ), atn( ) = (inverse tangent), sqr( ) = (square root) are some of the built-in Qbasic functions.
      Exercise : write a program to calculate square root:
      Ans :
                         
               L20:
                   input "Please enter number whose square root is needed ";a
                   b= sqr(a)
                   print "The square root of ";a;" is = ";b
                   goto L20
               
                          

      (Download program)

  4. When we have finished writing Qbasic program, and wish to quit,
    Alt -> File -> Exit
    Then we escaped from Qbasic, and into DOS.
    To escape from DOS and back into Window, type the DOS command :
    exit
    and we will be back to Window.

  5. When we are in DOS ( = Disk Operating System ), we may use DOS commands, e.g. " type a:\myfile.bas " then DOS will output the content of that program onto the screen,
    " del a:\myfile.bas " ( del = delete ) then DOS will delete this file.
    " dir a: " (dir = directory) tell DOS to list the names of all files on floppy.
    " dir C: " = list the names of all files on hard drive.

    The commands that we use daily are very few, and they will be discussed more fully in Chapter 5.

  6. If you have a printer, then you may use "lprint" ( = line printer print ) instead of "print" ,e.g.
                lprint "Area of triangle is ";area
    
    After printing, press "resume" button on the printer to roll out the paper. (Note : to control a printer fully, e.g. left margin, right margin, top margin, no. of lines per paper, no. of lines per inch, font, size, .... You will have to use "printer control codes" , e.g.
            lprint chr$(27);"C";chr$(60)         --- set 60 lines per page
            lprint chr$(27);"l";chr$(7)          --- left margin starts at column 7
            lprint chr$(27);"Q";chr$(72)         --- right margin ends at column 72
            lprint chr$(13)                      --- leave one blank line
            lprint chr$(12)                      --- eject paper.
    
    The above control codes are for "Epson LQ-2550" printer; but as many printers, e.g. those by Canon, also support this code, you may use it on other printers too. These control codes are usually in "programmer's manual" and you will have to buy it from dealers or manufacturers.

  7. Note the use of label
          label_name :
    
    which should be on separate line, with a label name, followed by ":".

  8. In the next lesson, we will talk about
         
                   if  ..... then
                        ....
                        ....
                   elseif ..... then
                        ....
                        ....
                   elseif .....  then
                        ....
                        ....
                   else
                        ....
                        ....
                   end if
    
    " if, then, elseif, else, endif " are all keywords. Coupled with " input, print, goto " you can already write many useful programs !

 [Home][Next]