Monday, February 2, 2015

Beginner's guide to QBasic

QBasic is Microsoft's version of the BASIC programming language. Developed in 1987, it was shipped out as part of the MS-DOS operating system. Now you can usually find a copy online somewhere to download.

As far as programming languages go, BASIC is very easy to learn. And QBasic is even easier, because it runs in its own editor and formats your code for you. For example, to make the computer print the word "HELLO," the command in BASIC is:

PRINT "HELLO"

In QBasic, you could type it as follows, and the editor would automatically capitalize your command and add a space before the quotation mark:

print"HELLO"

PRINT is one of the most common commands in BASIC. It can be abbreviated with a question mark (ie, ?"HI") in QBasic, to save you time writing code.

QBasic reads code starting from the top and working its way down, unless you tell it to specifically jump to another line. Line numbers were required at the beginning of every line in older BASIC programs, but for QBasic, you only need them if you want to reference that line somehow (for instance, by telling the program later to go back to that line).

This may seem confusing, so let's take a look at a very simple program. If you have QBasic, type the following lines of code in and run them.

CLS: ?"Let's count to 10."
a=0
5 a=a+1:? a
if a<10 then goto 5
end

You'll notice that as you type each line in and hit <ENTER>, the editor will format that line (add spaces between the letters and symbols, change ? to PRINT, and make all of the commands capital letters).

If you run this program, the screen would clear and you would see the following text:

Let's count to 10.
1
2
3
4
5
6
7
8
9
10

Let's break down the commands used in this program, and then we'll take a look at how line numbers are used in QBasic.

The first command is CLS. This stands for "clear screen," and does just what it says. It's a good command to use at the beginning of a program, in case there is already other text on the screen. The colon after the command tells the editor that you are putting another command on the same line.  You can put as many commands on one line as you want, as long as they are separated by colons.

The "?" is just the PRINT command, followed by what you want to print.

On the next line, we start to get into variables. Variables are what you learned about in algebra -- letters (or letters plus numbers, such as a1, a2, etc) that are assigned a numeric value. In this case, "a" is assigned the value 0. (You can also make string variables, which we'll discuss later.)

Now we come to the first line number. We make "a" equal to its value plus 1, then we print it. Notice you don't need to put quotes around the variable when printing it, because it's not a string of text -- you don't want to print the letter "a," you want to print its value.

On the next line we have the IF...THEN command. IF...THEN is a powerful tool you can use to allow your program to make choices. Say you're writing a simple game like "guess what number I picked" -- depending on whether or not the player guesses the right number, the computer can respond with "correct" or "incorrect."

In this program, the first time the IF...THEN statement is reached, the value of "a" is 1. 1 is less than 10, so the program executes the code that follows the THEN command; in this case, the GOTO command tells it to go to line 5. Line numbers can be any number, or even a word or string of words (such as BeginningLine or WrongAnswer). If you use words for line numbers, you have to follow them with a colon.

This program will keep printing the value of "a" and returning to line 5 until "a" is not less than 10. Then the program will end, and you will see "Press any key to continue" at the bottom of the screen. Once you hit a key, you will be returned to the editor.

Variables are very useful in BASIC because you can either assign them values that can be changed, or allow the user to assign them values. Numeric variables are simply letters (or letters and numbers, such as a4) but to assign a variable text (or a string of text) you would have to make it a string variable, which is designated by a dollar sign at the end of the variable name (ie, name$).

A useful command that involves variables is INPUT. INPUT allows you to get information from the user and store that information as a variable. For example, executing this line:

INPUT "What is your favorite number?", num

will print the question in quotation marks and then prompt the user to type something and hit <ENTER>. "Num" is a numeric variable (to make it a text or string variable, you would have to name it num$), so entering text at the prompt will either generate a "Redo from start" error or assign the variable a value of zero.

(The line of code above uses a comma with the INPUT command. You can also substitute a semi-colon, which will tell the program to add a question mark and a space after it prints the text.)

String variables are used the same way as numeric variables. This short program makes use of a string variable:

INPUT "What is your favorite color?", color$
PRINT: PRINT "Your favorite color is " color$ "."

The user would be prompted to enter a color, and then the program would repeat that color back to the user. (The PRINT command with no text or variable following it will simply print a blank line.) The variable "color$" is outside the quotation marks in the PRINT command, because we want to print the variable's value.

The QBasic editor is built on the MS-DOS text editor, and is similar to Windows Notepad. You can use the mouse or keyboard to navigate it, and it comes with many helpful features, including, Find, Change, and Split Screen.

Now you know some useful QBasic commands that will help you get started writing simple code and becoming familiar with the language. QBasic comes with plenty of help if you get stuck -- a list of contents, an index, and even a "Survival Guide."

(Originally published on Helium.com, May 2009)

No comments:

Post a Comment