|
|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 4
| |||||||||||||||
| 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.
| D1 = Monday , | D2 = Tuesday , | D3 = Wednesday , |
| D4 = Thursday , | D5 = Friday . |
| D$(1) = "Monday" , | D$(2) = "Tuesday" , | D$(3) = "Wednesday" , |
| D$(4) = "Thursday" , | D$(5) = "Friday" . |
| E(0)= 0, | E(1) = 2, | E(2) = 4, | E(3) = 6, | E(4) = 8, | E(5) = 10. |
| 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.
| 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 = 2 | READ X, Y | READ X | READ X, Y | |||
| Y = 3 | DATA 2, 3 | READ Y | DATA 2 | |||
| DATA 2, 3 | DATA 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$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.
DATA Joe Smith, junior, 22, 154, "Honolulu, Hawaii"
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 EvansBe certain that data types in your DATA statements match variable types in your READ statements. The sequence
DATA Freda Finley, Greta Garbo, Hilary Hill, Inez Ingall, Jan Joplin
| READ A$, B |
| DATA horse, buggy |
produces a "Syntax error" message, because "B" is a numeric variable but "buggy" is a string.
| 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.