QBasic Tutorial

Menu

Back to tutorial menu

QBasic Tutorial #2

Last time I left you with this little program.

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

To change this into a template file, just remove the PRINT line and leave a gap. This is the file you should continue to use from now on so save it as template.bas. On with some new stuff.

Our first program showed how to show a message to the user about the program. Now we can communicate with the user but very often, the user needs to give the program some kind of input. This is where the INPUT keyword comes in. This is how it looks.

' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
' Start of program
CLS
DIM a$
INPUT "Enter anything: "; a$
PRINT "You entered: "; a$
END
' End of program

The DIM statement is telling QBasic that a variable (a$) is going to be used as a container for a value (DIMensioned). A type is assigned using the dollar sign which in this case means a string of characters. The INPUT keyword displays the string in inverted comma's much like PRINT does (indeed it is possible to use PRINT if you wanted to). The semicolon simply means that the prompt (usually a question mark) will be displayed at the end of the string which is why there is a space at the end to make it look a bit nicer. The user then inpiuts anything (numbers and letters) and pressing the RETURN key in the usual way means that the next command is then interpreted. This then prints a message followed by the result of what was INPUT held in the variable a$.

Now anyone new to programming may be floundering a bit here so I'll just go through that again. First we clear the screen using CLS as before. Then we create a variable (a container) to take a value which in this case will be a string of characters that can include upper and lower case letters, numbers or any other ASCII character including SPACE or punctuation and this will be faithfully copied into the a$ variable. This will then be printed on the screen with a suitable explanation of what it is being displayed.

Now change the semicolons to commas and rerun the program. The comma in the INPUT line doesn't seem to do anything different to the semicolon. In fact, strictly speaking, the INPUT command only supports the semicolon but it still seems to work. The PRINT command however displays the contents of a$ padded out with extra spaces. The PRINT command uses the comma to display in print zones which are normally 14 characters wide. This can be useful if showing tables of numbers. Now a slightly different way of using PRINT. Open you template .bas file and add the following to it just below the CLS command.

DIM a
DIM b

INPUT "Enter a number: "; a
PRINT "Enter another number: "; b
PRINT "Your first number is: "; a
PRINT "Your second number is: "; b
PRINT "The sum of your two numbers is: "; a + b

which will then look like this:-

' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
' Start of program
CLS
DIM a
DIM b

INPUT "Enter a number: "; a
PRINT "Enter another number: "; b
PRINT "Your first number is: "; a
PRINT "Your second number is: "; b
PRINT "The sum of your two numbers is: "; a + b
END
' End of program

When run, the program asks for two numbers, displays them and then adds them together and displays the result with a helpful message. Did you use only single (integer) numbers such as 1 and 4? If so just try it with floating point (decimal) numbers such as 1.2 and 3.4. The DIMensioned variables can contain both types (although they can be fixed to only a particular type). Now run the program again and try to enter characters (like we for example) instead of numbers and the program will complain and you will be forced to continue until you enter a number. Incidentally, you can break out of a program at any time using CTRL BREAK.

That completes this second tutorial. We'll get into something a little meatier next time.

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