|
|
xxxxxxxxxxxxxxxxxx |
MORE BASIC INFO
| |||||||||||||||
| CONST pi = 3.14159 |
tells Basic that pi is a constant with the value 3.14159. If later in the program you want to compute the area of a circle of radius R, you can use a statement like
| A = pi * R * R | . |
If you want to use double precision in the constant pi, write
| CONST pi = 3.1415926536# | . |
If you do not specify the type after a constant, Basic will sometimes make an intelligent guess to specify a type for you. A constant can be defined also as a string, such as
| CONST myname = "Don Ho" | . |
Once a constant is defined, you cannot perform operations in the program that change the constant. For instance, if you define pi as 3.14159 and later add the statement
| pi = pi + 2 | , |
you will get an error message.
| LPRINT "The solution of the equation is"; X |
gives printing instructions to the printer connected to your computer. The output appears on paper exactly as it would on the screen if LPRINT were changed to PRINT. You can use the USING command also with LPRINT, as in
| LPRINT USING "Your refund is $$####.##"; R |
(The letter L before print comes from the phrase "line printer", used in the old days to refer to the printer connected to the computer.)
| RANDOMIZE TIMER |
somewhere before the RND statement. The sequence of commands
| RANDOMIZE TIMER |
| X = RND |
| M = INT(RND * N) + 1 |
produces a random number X between 0 and 1, and a random integer M from 1 to N.
| INT(X) = largest integer less than or equal to X |
| FIX(X) = whole number part of X (drops the part to the right of the decimal point) |
| CINT(X) = closest integer to X (rounds X to the closest integer) |
| CLNG(X) = closest long integer to X (handles bigger numbers than CINT) |
Examples :
| INT (2.4) = 2 | FIX (3.9) = 3 | CINT (2.3) = 2 | ||
| INT (5) = 5 | FIX (6) = 6 | CINT (8.54) = 9 | ||
| INT (-3.72) = - 4 | FIX (-7.21) = - 7 | CINT (-1.72) = - 2 |
But sometimes it is necessary that a subprogram remember the values of some or all of its variables from one run to another. For example, if the subprogram has a print counter P, counting the number of times the subprogram prints something, then you will not want P to reset to zero each time the subprogram is called, but rather you will want P to retain its value between the end of one run to the beginning of the next run. A variable in a subprogram having this property is called a static variable.
The STATIC statement is used to make variables static - that is, it forces variables to preserve their values from one run of a subprogram to the next. If you place the word STATIC at the end of the first line of a subprogram procedure, or of a function subprogram, then all variables in the subprogram are static. Here are examples:
| SUB Poly (N,X) STATIC |
| DEF FNA (X) STATIC |
If you want only certain variables in the subprogram to be static, you may insert a STATIC statement within the subprogram to accomplish this. The statement
| STATIC X |
makes X a static variable in a subprogram, while the statement
| STATIC Y, A() |
makes Y a static variable and A a static array. (The array must be followed by empty parentheses.)
| RESTORE |
(on a line by itself) instructs Basic to begin reading the DATA statements again from the beginning. It might appear in a sequence like the following:
| INPUT "Do you want to repeat the program"; ans$ | |
| IF ans$ = "y" THEN | |
| RESTORE | |
| GOTO Begin | |
| END IF | |
| PRINT "OK - Bye" | |
If you want to begin reading data following a certain label in the program, then you may modify the RESTORE statement to
| RESTORE Label |
where in the place of "Label" you insert the desired label.
| SELECT CASE variable or expression | |
| CASE IS condition 1 | |
| instructions | |
| CASE IS condition 2 | |
| instructions | |
| ····· | |
| and so on | |
| ····· | |
| CASE IS condition N | |
| instructions | |
| END SELECT | |
Example :
| INPUT "What is your age"; age | ||
| SELECT CASE age | ||
| CASE IS < 18 | ||
| PRINT "You can neither vote nor retire" | ||
| CASE IS < 65 | ||
| PRINT "You can vote but you cannot retire" | ||
| CASE IS >= 65 | ||
| PRINT "You can vote and you can retire" | ||
| END SELECT | ||
In the example, "CASE" takes the value of "age" and the conditions are checked. Basic executes the instructions following the first condition checking true, and no others. Thus, although a person of age 5 would satisfy both conditions CASE < 18 and CASE < 65, only the instructions under CASE < 18 are carried out since this condition is listed first.
If it happens that none of the conditions in a SELECT CASE procedure check out as true, then no instructions are carried out and Basic proceeds to the next statement in the program. Sometimes a last case, labeled CASE ELSE, is placed at the end of a SELECT CASE procedure in order to take care of all miscellaneous cases that might occur but were not listed. Here is an example:
| Prompt: | |
| INPUT "Do you want to repeat the program (enter y or n)"; ans$ | |
| SELECT CASE ans$ | |
| Case = "n" | |
| PRINT "OK - Bye" | |
| END | |
| CASE = "y" | |
| GOTO Begin | |
| CASE ELSE | |
| PRINT "You did not follow the instructions" | |
| GOTO Prompt | |
| END SELECT | |
Note that if the user errors and enters any letter besides "y" or "n", then the miscellaneous "ELSE" case takes over and returns the program back to the prompt.
You may also specify a range of values as a condition in a SELECT CASE procedure, as the following example illustrates:
| INPUT "What is your age"; age | |
| SELECT CASE age | |
| CASE 0 TO 12 | |
| PRINT "You are a child" | |
| CASE 13 TO 19 | |
| PRINT "You are a teenager" | |
| CASE 20 TO 39 | |
| PRINT "You are a young adult" | |
| CASE 40 TO 59 | |
| PRINT "You are middle-aged" | |
| CASE ELSE | |
| PRINT "You are a senior citizen" | |
| END SELECT | |
If age < 13 then the first case instructions are followed, while if 12 < age < 20 the second case instructions are carried out, etc. The "ELSE" case includes all ages 60 and above, as these are the ages not included in any of the preceding ranges.
| WHILE condition |
| instructions |
| WEND |
As long as the condition is satisfied, the loop continues and the instructions are carried out. The WEND statement marks the bottom of the loop.
The construction below adds the integers from 1 to 10 and prints the result:
| N = 1: sum = 0 | |
| WHILE N <= 10 | |
| sum = sum + N | |
| N = N + 1 | |
| WEND | |
| PRINT "The sum is "; sum | |
The WHILE … WEND loop is a holdover from earlier versions of Basic; it is retained in later versions only because old-time programmers are already familiar with it. However, the loop is unnecessary as it has been superseded by the more versatile four versions of the DO … LOOP. You are probably better off not using it.
| Exit Statement | Purpose | |
| EXIT FOR | Exits FOR … NEXT LOOP | |
| EXIT DO | Exits DO … LOOP | |
| EXIT SUB | Exits Subprogram | |
| EXIT DEF | Exits Function Definition |
An EXIT from a subprogram or a function definition produces a jump to the next statement in the main program following the calling of the subprogram or definition.
| FOR K = 1 TO 100 |
| PRINT K ^ 2 |
| NEXT K |
The problem with this simple program is that the data will not be aligned nicely in columns. Moreover, if the PRINT statement is changed to PRINT# so that the data is printed to a file instead of the screen, then output will run off the right side of the screen when the file is viewed in Basic.
To align the data into columns we insert a print counter P, keeping track of the number of print operations. We include also in the loop a TAB statement informing Basic in which column to begin the next print, depending on the value of P. One possible way of doing this is with the sequence
| P = 0 | |
| FOR K = 1 TO 100 | |
| PRINT TAB(1 + 10 * (P MOD 8)); K ^ 2 | |
| P = P + 1 | |
| NEXT K | |
The first time through the loop, P = 0 and P MOD 8 = 0, so that data is printed in column 1. The second time through the loop, P = 1 and P MOD 8 = 1, so data is printed in column 11. The third time through the loop, P = 2 and P MOD 8 = 2, and data is printed in column 21. Each time through the loop the column for printing moves 10 spaces to the right, all the way up to the eighth time through the loop when P = 7. But the ninth time through the loop, when P = 8, then P MOD 8 = 0 again, so printing returns back to column 1 and thus to the next line.
The upshot of all this is that we get 8 vertical columns of data, and the leftmost edges of these columns are 10 Basic columns apart. (Since Basic uses only 80 columns, we must be careful not to specify a printing beyond that number - doing so would mess things up.)
More generally, if you want to print K vertical columns of data, with leftmost edges M Basic columns apart, you would change the TAB statement to
| TAB(1 + M * (P MOD K)) |
But you should choose M and K so that MK is no larger than about 80, as otherwise you risk running off the right side of the screen.