QBASIC Commands and Instructions

An important component of this course, along with the problem sets and discussions, is some very rudimentary computer programming that will give us insight into mathematical concepts and also teach us some basics of computer technology.

The following are a set of QBASIC commands and instructions that are useful for the programming assignments in MA17. There is a more complete list in the MA17 Manual, but for the programming that we will be doing in this on-line version of the course, all you will need are the programming elements given here. Remember that this is not a programming course, but rather a course in computer math concepts that uses rudimentary programming to illustrate concepts in math and computers, and the relationship between the two. Use this page to begin learning the QBASIC programming language. Print out this short user's manual and type in all of the sample programs I have included in the descriptions below.

I have used a courier (non-propotionally spaced) font for this page. That is so that the programming examples will look the same here as when you type them into the QBASIC window on your computer. QBASIC runs in a DOS window under Microsoft Windows. If you are using a Macintosh computer, as I am, then you cannot run QBASIC unless you have Virtual PC on your machine. If you have a Mac and you don't know what I am talking about here, you must contact me immediately.

DOWNLOAD AND INSTALLATION OF THE QBASIC SYSTEM

To get QBASIC installed on your system you must download a compressed file (so-called "zip" file), and then decompress it (unzip it) on your disk. If you have an unzip program on your system, it is straightforward to download the zip file using the link below, and then douple-clicking on the downloaded folder to decompress the files contained in it. If you don't have an unzip program installed on your system, you will have to download and install that first.

The link for downloading the complete QBASIC system is

http://www2.sunysuffolk.edu/russom/images/qbasica.zip.

The link for downloading the unzip program is

http://www2.sunysuffolk.edu/russom/images/winzip95.exe.

Once you have downloaded and installed QBASIC, try typing the sample programs given below into QBASIC, and executing them. If you have difficulties, let me know and I will try to help you. You can also get help from any of the campus math skills or academic computing centers.

Following you will find the QBASIC reference material that you need for this course. Please let me know if you have any corrections or suggested additions or improvements.

ARITHMETIC OPERATORS

Exponentiation (^)
Raises an expression to a power.

This symbol (^), called a carot, is on the same key as the number six on your keyboard. The exponentiation operator allows you to raise numbers to a power. For example x = y^2 will take the contents of location y, square it and store the result in location x. Try the following simple program:

CLS
Y = 2
X = Y^4
PRINT "The result of this computation is ",X
END

Multiplication (*)
Multiplies two expressions

This symbol (*), called an asterisk, is on the same key as the number eight on your keyboard. The mulitplication operator allows you to multiply two numbers. Try the following simple QBASIC program:

CLS
X = 5
Y = 7
Z = X*Y
PRINT "The product is ", Z
END

Division (/)
Divides one expression into another

This symbol (/), the forward slash, is on the same key as the question mark. The division operator allows you to divide two numbers. Try the following simple QBASIC program:

CLS
X = 10
Y = 7
Z = X/Y
PRINT "The quotient is ", Z
END
Try this program again, only set Y = 0 instead of Y = 10 and see what happens.

Addition (+)
Adds two expressions

The plus sign (+) is on the same key as the equal sign. The addition operator allows you to add two numbers. Try the following simple QBASIC program:

CLS
X = 5
Y = 7
Z = X+Y
PRINT "The sum is ", Z
END

Subtraction (-)
Subtracts two expressions

The minus sign (-) is on the key next to the plus sign. The subtraction operator allows you to subtract two numbers. Try the following simple QBASIC program:

CLS
X = 5
Y = 7
Z = X-Y
PRINT "The difference is ", Z
END

FUNCTIONS

ABS( )
Returns the absolute value of a number.

Example: X = ABS (25-65) The location corresponding to X will contain the number 40. Try the following simple QBASIC program:

CLS
X = 5
Y = 7
Z = ABS(X-Y)
PRINT "The absolute value of X-Y is ", Z
END

SQR( )
Returns the square root of a numeric expression.

Example: X = SQR(25) The memory location corresponding to X will contain the number 5. Try the following simple QBASIC program:

CLS
X = 18
Y = SQR(X)
PRINT "The square root of X is ", Y
END

Try this program a few times with different values assigned to the variable X. Try, for example, X = -25 and see what happens.

LOGICAL OPERATORS

The AND operator does the same job as the logical "and" connective from mathematical logic. The logical expression (condition1 AND condition2) is true only if both condition 1 and condition 2 are true.

Example:

(1 < 5) AND (6 < 7) is true
(1 < 5) AND (6 < 5) is false

ORLogical "or"

The logical expression (condition 1 OR condition 2) is true only if either condition1 or condition2 are true, or if both condition 1 and condition 2 are true.

Example:

(1 < 2) OR (7 > 8) is true
(5 < 3) OR (7 < 2) is false

STATEMENTS

CLSClear Screen
Clears the screen and positions the cursor at the upper left of the screen.

DIM Declares an array.

The statement DIM arrayName(upper) declares an array with subscripts ranging from 0 to upper, where upper is an integer.

Example: DIM xarry(30) defines an array that is 1 by 30 words long.

A statement DIM arrayName(lower, upper) declares a doubly subscripted, or two-dimensional, array. Example: yarray(6,10) defines an rectangular array with 6 rows and 10 columns.

END End a program, procedure, block, or user-defined data type.

INPUT Allows program user to input numbers or character strings to a program.

The statement INPUT var causes the computer to display question mark and to pause until the user enters a response. This response is then assigned to the variable var.

A statement of the form INPUT "prompt"; var inserts a prompting message before the question mark.
A statement of the form INPUT "prompt", var displays the message without the question mark.
In each of these statements var may be replaced by a number of variables separated by commas. After the user enters the values requested, each value is assigned to the corresponding variable.

Example:

INPUT "Enter your name"; name$
Output: Enter your name? enters name and type return

PRINTThis instruction writes data to the screen .

PRINT expressionlist (list items separated by comma or semi-colon)
Expressionlist is a list of one or more numeric or string expressions.
Comma or semicolon determines where the next output begins:
Comma (",") means print at the start of the next print zone (each print zone is 14 characters wide.)
Semicolon (";") means print immediately after the last value.

Example:

PRINT "This is a test";" only a test"
Output: This is a test only a test

PRINT "This is a test", "only a test"
Output: This is a testonly a test

REMRemark, this allows comments to be inserted in a program (the instruction is not executed). The REM command can be replace by an apostrophe.

Example:

REM The next six lines of code get input from the user. ‘The next six lines of code get input from the user.

LOOP STRUCTURES

Pre-test Loop

DO WHILE condition ....
.
.
.
LOOP

This loop structure does the testing at the beginning of the loop, if the condition is met, then the statements within the loop will be executed. The repetition will stop once the condition is no longer true.

Consider the following QBASIC Program:

CLS
X = 0
I = 0
DO WHILE I<10
I = I + 1
X = X + I
PRINT X
LOOP
END

Post-test Loop

DO
.
.
.
LOOP UNTIL condition

This loop structure does the testing at the end of the loop, if the condition is satisfied, then the loop is terminated, otherwise, loop continues.

Consider the following QBASIC Program:

CLS
X = 0
I = 0
DO
I = I + 1
X = X + I
PRINT X
LOOP UNTIL I > 9
END

Loop that is executed a preset number of times

FOR variable = initial TO limit STEP increment
.
.
.
NEXT variable

In this structure, variable serves as the counter. It is initialized to the number specified by initial, incremented by the number specified after the word STEP, and ends the loop when variable is equal to or more than what is specified by limit. If STEP is not specified, the default is the counter will be incremented by 1. STEP could also decrement instead of increment.

Consider the following QBASIC program:

CLS
X = 0
FOR I = 1 TO 10
X = X + I
PRINT X
NEXT I
END

DECISION STRUCTURES

IF statement

IF condition THEN action

A statement of the above form causes the program to take the specific action if condition is true. Otherwise, execution continues at the next line.

Consider the following QBASIC program:

CLS
10 INPUT "Type in the word first, second, or end ",ORDER$
   IF ORDER$ = "first" THEN 20
   IF ORDER$ = "second" THEN 30
   IF ORDER$ = "end" THEN 50
   PRINT "Bad type in, try again"
   GOTO 10
20 PRINT "You typed first"
   GOTO 10
30 PRINT "You typed second"
   GOTO 10
50 END

This program gives the basic structure for providing inputs to a program and using them to do calculations and then printing out answers. It is a structure that will be used in almost all of the programs that we will create in this course.

IF THEN ELSE IF condition THEN action1 ELSE action2

A statement of the above form causes the program to take action1 if condition is true and action2 if condition is false.

BLOCK IF

IF condition1 THEN
Statement1
Statement2
Statement3
.
.
.
END IF,

A block of statements such as the one above indicates that the group of statements between IF and END IF are to be executed only when condition is true. If the group of statements is separated into two parts by an ELSE statement, then the first part will be executed if the condition is true and the second part will be executed when the condition is false.

Gonsider the following QBASIC program containing a block if statment:

   CLS
   REM
   REM This program divides two numbers input
   REM by the user and then ends
   REM
10 INPUT "Type in two numbers, x,y "; x, y    REM
   REM Check for divide by zero
   REM
   IF y = 0 THEN
   PRINT " "
   PRINT "Cannot divide by zero"
   PRINT " "
   GOTO 10
   END IF
   z = x / y
   PRINT " "
   PRINT "x/y = ", z
   END