Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 4

Lesson Topics
String VariablesREAD
ArraysDATA
DIMTAB
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 1 # 2 # 3 # 4 # 5 # 6 # 7
Main Basic Page

String Variables

A string variable consists of a sequence of characters, called a string. The name for a string variable must end with the dollar sign "$". Here are some examples of names of string variables with their values:

W$ = "Washington"
HAMLET$ = "to be or not to be"
PROF$ = "Higgins".

Although a string is always enclosed in quotation marks, the quotation marks are not considered part of the string - they are only a device informing Basic that the characters form a string as opposed to a numeric variable. A string can contain spaces as well as letters, numbers, and punctuation marks. A string can be up to 32,767 characters long - long enough for most purposes!

A plus sign between strings combines the strings into one string. If we let B$ = "bay" and W$ = "watch", then

B$ + W$ = "bay" + "watch" = "baywatch" .

If you want to combine two strings but with a space between them, then you insert a string containing a space, as in

B$ + " " + W$ = "bay" + " " + "watch" = "bay watch" .

Do not use the names DATE$ or NAME$ for strings, as these are reserved by Basic for other purposes. Likewise, do not use a Basic command as a string name - for instance, the names PRINT$, END$, and INPUT$ are illegal.

Arrays

You can think of an array as just a list. In mathematics a list is often denoted with subscripts. A mathematician might refer to the five weekdays as
D1 = Monday , D2 = Tuesday , D3 = Wednesday ,
D4 = Thursday , D5 = Friday .
Because Basic has no subscripts, parentheses are used instead. In Basic we might refer to the above list of days as the string quantities
D$(1) = "Monday" , D$(2) = "Tuesday" , D$(3) = "Wednesday" ,
D$(4) = "Thursday" , D$(5) = "Friday" .
A list can consist also of numbers. The even numbers from 0 to 10 can be specified as
E(0)= 0, E(1) = 2, E(2) = 4, E(3) = 6, E(4) = 8, E(5) = 10.
Before working with an array you must introduce the array with a dimension statement, informing Basic of the length of the array. The dimension statement

DIM D$(1 TO 5)

informs Basic that D$ will be a list of strings, indexed by the integers 1 to 5. Also,

DIM X(10 to 99), N%(1 to 7), Y#(8)

declares that X will be a list of real numbers indexed by the integers 10 to 99, N% will be a list of integers indexed from 1 to 7, and Y# a list of double precision real numbers indexed from 0 to 8. (If only one positive integer appears in the parentheses, Basic by default takes 0 as the beginning integer.)

The beginning and ending subscripts of an array may be negative, and may also be variables. For instance, the dimension statement DIM C(-3,K) is perfectly legal. When Basic reads a dimension statement, it initially sets the values of all numbers in the array equal to zero; they remain at zero until the program changes them.

READ and DATA Statements

READ and DATA statements offer an alternative method of assigning values to variables. Below are two ways of assigning the variable X the value 2.

Method 1 :X = 2 Method 2 :READ X
DATA 2

Here are four ways of assigning X the value 2 and Y the value 3:

X = 2READ X, Y READ XREAD X, Y
Y = 3DATA 2, 3READ YDATA 2
DATA 2, 3DATA 3

When Basic comes to a READ statement containing a list of variables, it looks for a DATA statement containing a list of values. It reads the data values one at a time, in the order listed and beginning with the first DATA statement in the program. A DATA statement may appear anywhere in the program - Basic will find it. Some programmers place all DATA statements at the beginning of the program, some at the end, while others like to place a DATA statement right after the READ statement referring to it. All READ and DATA statements may refer to strings as well as numerical variables. Items in READ and DATA statements are separated by commas.

Example :

READ student$, class$, age, weight, homecity$
DATA Joe Smith, junior, 22, 154, "Honolulu, Hawaii"
In a DATA statement it is optional whether or not you put quotation marks around a string; however, if the string contains punctuation marks then you might need quotation marks to avoid ambiguity. If one typed just Honolulu, Hawaii in the above DATA statement, then Basic would interpret Honolulu and Hawaii as two different strings.

If a DATA statement is too long for the screen, divide it into two statements, as below:

DATA Ann Ames, Betsy Bates, Cindy Crim, Diana Dees, Eve Evans
DATA Freda Finley, Greta Garbo, Hilary Hill, Inez Ingall, Jan Joplin
Be certain that data types in your DATA statements match variable types in your READ statements. The sequence

READ A$, B
DATA horse, buggy

produces a "Syntax error" message, because "B" is a numeric variable but "buggy" is a string.

TAB

A TAB command instructs Basic in which column to begin its next printing. The line

PRINT TAB(20); X

will print the value of X beginning in column 20 of the screen. (A Basic screen is meant to be 80 columns wide, with columns labeled from left to right as 1, 2, 3, …, 80.) The line

PRINT TAB(10); "Name:"; TAB(25); "Mary Jones"

prints Name: beginning in column 10, and Mary Jones beginning in column 25. A TAB command appears only in PRINT statements, and it must always be followed by a positive integer (no larger than 80) in parentheses.

TAB commands are especially useful in lining up columns in tables.