Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 1

Lesson Topics
REMINPUT
CLS User Friendliness
ENDASSIGNMENT
Numeric VariablesMathematical Operations
PRINT
View DemosDownload Demos
# 1 # 1
Main Basic Page

REM

Placing REM at the start of a line tells Basic that the line is only an information line - it has nothing to do with the program's execution but serves as information for the person reading the program. Basic ignores the line when running a program.

Examples :

REM This program solves a quadratic equation.
REM This routine computes the determinant of a matrix.
Do not make a REM statement so long that it runs off the right side of the page - instead divide it into two or more REM statements, as in the example below:
REM This program solves a cubic equation.
REM The user inputs the coefficients of the equation,
REM and the program computes the roots.

CLS

This command clears the monitor screen. It usually appears as one of the first lines of a program, so that when the program runs it begins with a blank screen.

END

This command announces the end of the program; execution stops when it is reached. While the last statement in the program should always be an END statement, a program can in fact have several END statements.

Numeric Variables

You can think of a numeric variable as sort of a memory box within your computer that holds a number; the number in the box can change during the running of a Basic program. The box must have a name so that it can be referred to in the program. The name for a variable can be anywhere from 1 to 40 characters long; it must begin with a letter and may contain letters or numbers (but no spaces). The only other restriction is that you cannot choose as a variable name some word that already has some purpose in Basic, such as the name of a command like REM or CLS or END, for example. Some possible names are X, Y, Z, area, radius, age, etc. You may introduce a numeric variable by simply referring to it in the program - Basic will by default give it the value 0 unless you specify otherwise. You can also introduce the variable by giving it a value, for instance with a statement like "X = 6". Or you can introduce a variable by defining it in terms of other variables, such as in a formula like "area = base * height".

PRINT

This command prints values of variables or text items to the screen.

Examples :

PRINT X
PRINT X; Y; Z
PRINT X, Y, Z
PRINT "THE ROOT OF THE EQUATION IS"; X;
PRINT "The diameter is"; 2 * r; "and the area is"; 3.14 * r * r
PRINT
The first example prints the current value of the variable X to the screen. The second prints the values of X, Y, and Z separated by a couple of spaces, while the third (with commas instead of semicolons) prints these same values but in different columns 14 spaces wide. In the fourth example, the text enclosed within quotation marks is printed verbatim, and then the value of X follows immediately; the semicolon following X ensures that the next item printed will follow directly after X and on the same line. (In the first three examples the next printing will begin on a new line.) In the fifth example, text inside quotes prints verbatim, while the mathematical expressions are evaluated using the present value of r and their values printed. The last print statement, on a line by itself, serves as a line feed - Basic will skip a line before the next printing.

INPUT

This command stops the program, prints a question mark to the screen, and waits for the user to type in a value for one or more variables. The user enters the value(s) and the program continues. Usually a prompt precedes an input statement, so that the user knows what variable to type.

Example :

PRINT "What is the interest rate";
INPUT R
(The semicolon at the end of the PRINT statement puts the question mark on the same line as the prompt - without it the question mark appears on the next line.) As in the next example, it is possible to ask for several items at once - the user types the values, separated by commas, before a final enter.

Example :

PRINT "What are the X, Y, and Z coordinates";
INPUT X, Y, Z

User Friendliness

You cannot assume the user knows what letters you use to represent quantities in your program. Do not prompt the user with a sequence like

PRINT "WHAT IS R";
INPUT R

as the user most likely will not know that R refers to the interest rate. Likewise, instead of informing the user of the value of the monthly payment with a statement like

PRINT "P = "; P

you should write

PRINT "THE MONTHLY PAYMENT IS "; P

Never print a naked quantity to the screen without explanation. The command

PRINT X

will print the value of X to the screen, but the user will have no idea what it signifies.

It is good idea to end a program with "Bye" or something to that effect, so that the user knows the program has terminated - otherwise, the user might presume that the computer is still working or is hung up on something.

Assignment

The equal sign gives the variable on the left hand side the value of the variable or expression on the right hand side.

Examples :

X = 6 (assigns X the value 6)
Y = Y + 2 (replaces Y with its old value plus 2)
Z = 3X + 2Y + 4 (assigns Z the current value of 3X + 2Y + 4)
The order is important - do not write a statement like

6 = X

as 6 is a constant and it cannot be assigned a new value. The statement

X = Y

assigns X the present value of Y - it does not assign Y the present value of X.

Mathematical Operations

The five elementary mathematical operations in Basic are indicated below.
Addition : X + Y
Subtraction : X - Y
Multiplication : X * Y (Don't type XY; Basic will think XY is a new variable.)
Division : X / Y
Exponentiation : X ^ Y (Eg. : 3 ^ 2 = 32 = 9)
In the absence of parentheses, exponentiation is performed first, then multiplications and divisions, and lastly additions and subtractions. But when in doubt - use parentheses!!