Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 5

Lesson Topics
DEFtype StatementsGOTO
LabelsPRINT USING
View DemosDownload Demos
# 1 # 2 # 3 # 1 # 2 # 3
Main Basic Page

DEFtype Statements

If you are using lots of variables of different types in a program, it might be easier to use DEFtype statements to inform Basic of the types of all your variables; this eliminates the hassle of ending variables with characters like !, #, %, &, $, etc. All DEFtype statements must be placed near the beginning of a Basic program, preceding any executable statements (i.e, before the program starts taking any actions).

Examples :
DEFINT I-L Informs Basic that any variable starting with any of
the letters I through L (i.e., I, J, K, L) is an integer.
DEFSNG A-C, X Variables starting with A, B, C, or X are single precision.
DEFDBL Z Any variable starting with Z is double precision.
DEFSTR S-V Variables beginning with S, T, U, or V are strings.
DEFLNG K, M Variables beginning with K or M are long integers.

As illustration, the program

DEFDBL R
root = SQR(2)
PRINT "The square root of 2 is"; root

gives the double precision output

The square root of 2 is 1.414213562373095.

Labels and GOTO

A label in a program serves as a marker. A label can be up to 40 characters long, and may contain letters and digits. It cannot have any spaces, a colon must follow it, and it stands alone on the line. Here are some legitimate labels:

Begin: A6: Kentucky: .

Labels most often appear with GOTO statements. A GOTO statement directs Basic to jump to a label and continue the program at the label. Here are some GOTO statements:

GOTO Begin GOTO A6 GOTO Kentucky .

Here is a simple program using a GOTO statement:

REM This program exercises your typing finger.
Again:
INPUT "Is your finger tired (type y or n)"; ans$
IF ans$ = "n" OR ans$ = "N" then GOTO Again
PRINT "OK, you can stop"
END

As long as the user continues typing "n" or "N", this program returns to the label "Again:" and repeats itself; otherwise the program prints "OK, you can stop" and ends.

Good programmers do not use too many GOTO statements, as they can make a program hard to follow. However, for some purposes they are quite useful and efficient.

PRINT USING

The PRINT USING statement gives you more control over the way numbers are printed. Among other things, you can specify the number of digits you want after the decimal point, and you may place a dollar sign before a number.

Instead of trying to explain all the rules, it is perhaps more efficient just to look at various examples. Let us suppose that X , E$, and S$ are variables with the values

X = 1234.56789 , E$ = "Ed" , S$ = "Sue" .

The examples below illustrate some uses of PRINT USING.

PRINT USING "#####.###"; X
Output : 1234.568
Substitutes X for #####.###, rounds to 3 decimal places

PRINT USING "$$#####.##"; X
Output : $1234.57
Rounds X to 2 places, leads with $ sign

PRINT USING "###.####"; X
Output : %1234.5679
Leads with %, a warning that 1234 is too big to fit the format ###.

PRINT USING "#####"; X
Output : 1235
Rounds X to 0 places

PRINT USING "##,###.##"; X
Output : 1,234.57
Inserts comma

PRINT USING "X = ####.#"; X
Output : X = 1234.6
Substitutes X for ####.#

PRINT USING "You owe $$####.##"; X
Output : You owe $1234.57
Substitutes X for ####.##, inserts $

PRINT USING "& owes $$####.##"; E$; X
Output : Ed owes $1234.57
Substitutes E$ for &, X for ####.##

PRINT USING "& loves & dearly"; E$; S$
Output : Ed loves Sue dearly
Substitutes E$ for &, then S$ for &

PRINT USING "& owes & $$####"; E$; S$; X
Output : Ed owes Sue $1235
Substitutes E$ for &, then S$ for &, then X for ####

The symbol "&" serves as a placeholder for a string variable, while a sequence of "#" symbols is a placeholder for a numeric variable. Basic prints whatever is enclosed inside the quotation marks, with appropriate substitutions of variables for placeholders. You should place enough "#" symbols left of the decimal point to handle the size of your output numbers. The #'s right of the decimal point specify the number of decimal places.