Computer Languages

Microsoft QBasic Interpreter

[ Back to Lecture Notes ] [ Chipmunk BASIC ] [ Yabasic ] [ JavaScript ]
[ Resources > BASIC Interpreters ]

Interpreted Program Translation

An interpreted computer language is a high-level language that is translated into binary machine code each time it is read from a source file. Disadvantages of interpreted languages are mainly that there is a translation time penalty each time the program is run, and the language interpreter must be present on each computer that will run the program. However, as a development tool interpreted languages are useful because a program prototype can be written and tested quickly, and since most interpreters step through the program line by line errors are easily localized and identified. Since most assemblers and compilers have a steep learning curve just to master the development environment interpreters are also ideal as learning tools.

CIS 120 is about computer concepts in general, not about programming in particular. With that in mind only the very essential QBasic statements, functions, and operators will be used. Recall that, as a minimum, we want a program to organize data, calculate results, and present the results. We will be looking at ways QBasic can get user input, manipulate the input (data), control program flow as needed, and produce output useful to the user. Consult QBasic Help for more details of all commands discussed.

[ TOP ]

QBasic Data Types and Operators

QBasic supports the basic data types discussed in the text book: integers, real numbers (floating point), characters, and Boolean as well as character strings, arrays, and records. We will only be working with the basic, sometimes called "primitive," types. In QBasic the character string is considered a basic type rather than a data structure. Data types are specified by a special character appended to the end of the variable name:

QBasic uses mostly familiar or intuitive operators, such as +, -, *,and / for addition, subtraction, multiplication, and division. An operator that may be new to you that is supported in virtually all programming languages is the Modulus division operator. Modulus division returns the remainder rather than the quotient. In QBasic the modulus operator is MOD. This differs from the "C Family" of languages that use % for modulus division. For example 19 MOD 4 is 3: 19 divided by 4 is 4 with a remainder of 3. Modulus division is very useful for many problems in computers; think back to our procedure for converting a decimal number to binary or hexadecimal. We started with the highest power of the base that could be divided into our decimal number, then kept working down by dividing the remainder by each successive lower power of the base.

Boolean conditions (named after George Boole) are tested with the familiar < and > symbols used in mathematics for "less than" and "greater than". "Less than or equal to" and "greater than or equal to" use the <= and >= operators respectively. Equality is tested with the = operator and inequality ( "not equal to" ) is tested with the <> operator. This differs from the "C Family" of languages that use -= for equality and != for inequality. Look at QBasic Help Contents under the topic "Basic Character Set." Note that the BASIC language uses the same operator ( = ) for assignment and equality; the interpreter knows the difference from context.

[ TOP ]

QBasic Input and Output

We will use the INPUT statement for program input and the PRINT statement for program output. The following QBasic statements will prompt the user for a name and then issue a greeting:

Output:

The INPUT statement takes an optional prompt string in quotation marks followed by a comma separated variable list. It is generally best to only fill one input variable for each INPUT statement. Notice how any extra spaces must be included inside the quoted strings in the PRINT statement, the semi-colons separating the PRINT arguments cause the arguments to run together.

You can use a semi-colon following the prompt string to have QBasic automatically add a question mark and a space, but I suggest you only use one form and explicitly write it as a question when desired. The following 2 INPUT statements are identical:

Output:

Note that the name variable had the "$" character appended to identify name as a string variable; use the "% " character for integer variables, "! " for single-precision variables, and "#" for double- precision variables. For the little programs we will write in this class you can just use single-precision for all numeric data types. Note also that the "'" character is the comment delimiter.

[ TOP ]

Conditional Control Statements

The QBasic control statement that allows a program to branch to different areas based on some condition is IF . . . THEN . . . ELSE.

Output: Another way to write the test condition would be using IF . . . THEN . . . ELSEIF: The IF . . . THEN . . . ELSEIF form makes it easy to add additional tests: Output:
[ TOP ]

Iteration (Loops)

The QBasic control structures that allows a program to repeat a certain section of code more than once based on the value of a condition are DO . . . LOOP and FOR . . . NEXT. All loops can be written with one basic form even if that one form is not always the clearest or most efficient. However, when the number of repetitions is known in advance, the FOR . . . NEXT loop is generally preferred. QBasic also supports the older WHILE . . . WEND loop construct.

Here is a simple QBasic loop using both forms:

Output:

This next loop is a bit more involved; it prompts the user for a number and saves it in a variable called sum, then enters a loop where it prompts for another number to add to sum. Note how, before entering the loop, the answer variable is pre-loaded to force at least one pass through the loop. At the end of each loop the user is asked if she wants to continue. Note also how the response is converted to upper case to simplify the test for a no answer.

Output:

Read the QBasic Help topic on the DO . . . LOOP. What other ways could this loop be written? Is another way more "logical" or more readable?

[ TOP ]

Strings

QBasic has some interesting and useful string manipulation functions. Research the LEN, MID$, ASC, and HEX$ functions in QBasic Help. Here is an example of how you might extract various pieces of a data record to build a string for a report:

Output:

Here is a way to handle extracting pieces of a string when you don't know where or how long the individual pieces are. Note how the string length function LEN is used as a loop upper bound control; then the MID$ extracts one character at a time, checking for a blank space. Each character is then concatenated (look it up in a dictionary!) to a new string.

Recall the discussion of ASCII character codes and the need to frequently know how to represent numbers in hexadecimal as well as decimal:

Output:

Now if we were to modify the above example to take input from the keyboard inside a loop it might actually be useful from time to time. Hmmm...

[ TOP ]

Where to Next?

Your best source of additional QBasic information is the QBasic Help system. Spend some time looking at the various topics in the Contents, in particular look at "Keywords by Programming Task" then go to the Index and select various keywords. Note that most keyword topics in the Index also show example code snippets. Copy the examples in this document into QBasic then experiment with modifications. You can "View Source," then copy and paste to QBasic. All example code is delimited by <PRE> and </PRE> tags.

Put a copy of qbasic.exe and qbasic.hlp (about 317 Kb for both files) in your working directory, or put them in a directory that is in your PATH. You can download the source files from the links below, or you can copy the code from the examples into a text editor and save them as BASIC source files (the usual extension for BASIC source files is .BAS). Execute a BASIC program in QBasic from the command line as follows:

Alternatively, start QBasic from a Command Prompt and use the File menu to load BASIC programs

The best way to learn a language, like learning to play a musical instrument, is to jump in and practice. Look at some examples in the Help topics for various keywords and try to put a few together to do something useful. However, unlike learning a new musical instrument, at least you won't annoy the neighbors or make the dog howl...

[ TOP ]

Example Source Code

Download example source code.