QBasic Tutorial

Menu

Back to tutorial menu

QBasic Tutorial #1

In the good old days (I'm definitely getting old) when RAM was measured in bytes rather than megabytes and processor speeds were measured in single figures, most operating systems came with a programming language. DOS had QBASIC, UNIX had it's shell scripting and most micros used some variation of BASIC. This gave users access to a programming language without the need to spend money. Magazines could publish program listings secure in the knowledge that users had access to the relevent language because it was bundled with the Operating System. Then along came the GUI (Graphical User Interface) and suddenly the facility dried up. QBasic is however is still a great way to get into programming despite the fact that it hasn't been updated to allow Windows Apps to be created (although VBScript can be used to do so - see WEBlications section of this site) and it is still shipped with Windows although it is tucked away in a folder callled OldDOS on the CD. This has lead to a fragmentation of the number of languages that users could be using (Java, VB etc...) and only available to those who paid good money for it (Java is free but a decent version complete with drag and drop IDE is not). I believe this has lead to most users not being able to write even the most rudimentary programs. It could be argued that such skills are not required and that the GUI does it all for them. Sadly, this is just not true. There are many tasks that benefit from programming skills which the GUI does not encourage. Here then is a good way to start learning to program and it really isn't that difficult.

To follow this tutorial, you will need a copy of QBasic which can be found on your Windows CD (95/98/ME) as above or somewhere on the hard disk if you have NT4 (I have yet to find out if it is still shipped with 2000 or XP). Create a new folder on your drive called QBasic and copy the following files to it:_

qbasic.exe
qbasic.hlp

Create a shortcut on you desktop to the qbasic.exe file and run the program. You will get something like this:-

QBasic main screen

Explore all the menu's in particular the help menu. Click on the About help menu to see what version of QBasic you are running - version 1.1 is the latest although version 1.0 for the purposes of this tutorial will do just as well.

Now BASIC in general has it's critics mainly because it is very easy to write very bad code and it will still work (after a fashion depending on how bad it is). It is also very easy to write very good code and it is this that I will be trying to encourage. If you are familiar with other versions of BASIC, you may try to start by entering a line number. You will be pleased to know that although line numbers still work in QBasic, they are not essential. This makes programming so much easier although there are pitfalls that need to be avoided. So on to our first program.

print "Hello Universe"

Note how the word print changes to all upper case PRINT.This is to show that PRINT is a keyword which in this case will print a string of characters to the screen (and NOT to a printer). Click on the Run menu then select start or hold down the SHIFT key and press F5 which will run the program (QBasic programs are interpreted a line at a time and don't need to be compiled first). This prints the text enclosed in the quotes and helpfully stops and asks for a keypress so you can see what the result of running the program was before returning to the editor screen. Press a key (the RETURN key is as good as any) to return to the editor. Congratulations! You've just run your first program. Now you may be a little disappointed. There are no buttons, text boxes or even mouse control. Remember, this is a DOS application and as explained above has not been updated to create Windows applications. So what use is that? You will be surprised just what can be achieved in QBasic (watch this space) and it is a valuable lesson in programming that will hold you in good stead when it comes to getting into more advanced techniques and Windows programming later.

Alright, so not the most exciting program in the world but it is your first after all. However, before we go any further, I am going to try to instill some structure into your programs right from the outset. This is relatively easy to do and involves writing a template file that you will use each time you write a new program. It will look something like this:-

' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
' Start of program
PRINT "Hello Universe"
END
' End of program

To get all this into QBasic is relatively easy. Highlight the text and then hold down the CTRL key then press the C key. This will hold the characters in the clipboard. Go to your QBasic DOS box and select what looks like a briefcase icon which when hovered over will give a Paste tooltip (it may need to be switched on in the properties of the box). Click on the paste icon and the characters will be transferred to QBasic. This can be saved as a program with the name hello.bas (the .bas part will be automatically appended to the name you type in). Run as above. If you haven't restarted QBasic then you will notice that the previous program's result (the word Hello Universe) are still visible. This is because the display needs to be cleared first using the keyword CLS which means CLear Screen. To alleviate this problem change the program in the QBasic editor to read:-

' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
' Start of program
CLS
PRINT "Hello Universe"
END
' End of program

There are several ways of showing comments one of which is the REM or Reminder statement. Really a hangover from previous versions of BASIC and not really used anymore. The other is an apostrophe as shown above. They can also be added between lines of code to explain what is happening although excessive use of comments can slow the code down a bit - not a problem on new machines but could give one of your users a problem if they are using older kit. We use comments to show what the program is doing (not a problem on small programs like this one but essential in really big programs) but also to show the structure of the program which will be more apparent as these tutorials progress.

The program is now complete. If you want to know more then view the next tutorial from the main menu.

  Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk