• Aucun résultat trouvé

BASIC Syntax

Dans le document the Programming (Page 23-27)

Alphabetical list of keywords. This section includes examples which beginners should try; experienced programmers will find it a useful reference.

Error messages. An annotated listing of error messages.

BASIC Syntax

BASIC is unquestionably the most popular computer language. It's far easier to write, test, and adjust than any other language, and simple programs can be written by people with very little experience.

BASIC is sometimes described as "like English," but the resemblance is tenuous.

At any level beyond the simplest, it has to be learned like any other skill. In addi-tion, it is a rather ad hoc language, so that some expressions work differently on dif-ferent machines. What follows applies to VIC BASIC.

Numerals and Literals

These are actual numbers and strings, not variables. Examples of the first are 0, 2.3E-7, 1234.75, and -744; examples of the second are "hello", "ABC123", and

"%!# /" where the quote symbols are delimiters (not part of the literal).

Numerals. The rules which determine the validity of these forms are complex.

Generally, numbers are valid if they contain 0-9,

+, -,

E and. in certain combina-tions. For instance, 1.23 is valid, but 1.2.3 is not (since only one decimal point may be used). Similarly, 2E3 is valid, but 2EE3 is not (since only one E is permitted).

Both DE and the lone decimal point are accepted as O.

Exponential notation (using E) may be unfamiliar, but it is not hard to master.

The number following E is the power of 10 that is multiplied by the number to the left of the E. In other words, it tells you how many places to move the decimal point left or right to produce an ordinary number. For example, 1.7E3 means "1 point 7 times 10 to the power 3," or 1.7"'1000, which is 1700. Similarly, 9.45E-2 means

"9.45 times 10 to the power -2," or 9.45"'1/100 (which is .0945). SHIFT-E is not ac-cepted. Values outside the ranges .01 to 999999999 and - .01 to -999999999 are output in exponential form.

Strings. Strings can contain any VIC ASCII characters. Tricky characters can be incorporated with the CHR$ function, including the double quotes character

(CHR$(34» and RETURN (CHR$(13». The maximum length of any string is 255 characters.

Variables

A variable is an algebraic idea. It can be thought of as a named symbol that is used to stand for a quantity or string of characters. X, X%, and X$, respectively, are a number variable (with a minimum value of ± 2.93873588E-39 and a maximum value of ± 1.70141183E38), an integer variable (some whole number between

-32768 and 32767), and a string of characters (containing from 0 to 255 characters).

If the variables haven't been assigned values, numeric variables default to O. Strings default to the null character, a string of zero length.

A variable, as the name implies, can be changed at will, as X = 1: PRINT X:

X = 2: PRINT X shows.

Variable names are subject to these rules and considerations:

1. The first character must be alphabetic.

2. The next character may be alphanumeric.

3. Any further alphanumerics are valid but will not be considered part of the name.

4. The next character may be % or $, denoting an integer or string variable, respectively.

5. The next character may be

C

denoting a subscripted variable.

6. A name cannot include reserved words, since the translator will treat them as keywords and tokenize them. Note that reserved variables (II and ST) can be incorporated in names, since they are not keywords. However, this is best avoided because it can lead to confusion. For example, a variable like START will be the same as ST.

Each of these rules serves to remove ambiguity and make storage convenient and fast. To appreciate their value, imagine a variable named 1A. If 1A were a valid variable name, 100 1A = 1 would require special syntactical treatment to distinguish it from 1001 A = 1. And if symbols other than alphanumerics were permitted, so that B= were a valid name, still other problems could appear.

Interconversion between variable types is automatic as far as numerals are con-cerned. However, string-to-numeric and vice versa require special functions. For in-stance, L % = L/256 automatically rounds L/256 and checks that the result is in the range from -32768 to 32767. Similarly, L$=STR$(L) and L=VAL(L$) or

L % = VAL(L$) converts numerals to strings and vice versa, subject to certain rules.

Two other interconversion functions are CHR$ and ASC, which operate on single bytes and enable expressions which would otherwise be treated as special cases.

Operators (Also Called Connectives)

Binary operators combine two items of the same type, creating a single new item of the same type. Unary operators modify a single item, generating a new one of the same type. The numeric operators supported by VIC BASIC are completely standard and are identical in type and priority to those of FORTRAN. The string operators and logical operators are rather less standard.

When a string expression or arithmetic expression is evaluated, the result de-pends on the priority assigned to each operator and the presence of parentheses.

Parentheses, in either string or arithmetic calculations, guarantee that the entire expression within parentheses is evaluated as a unit. In the absence of parentheses, priority is assigned to operators in this order (the highest priority operator is listed first):

BASIC Reference Guide

Binary plus and minus (addition and subtraction) Comparisons (less than, equal to, greater than) Logical NOT, unary operator

Logical AND, binary operator Logical OR, binary operator

Logical operators are also called Boolean operators. In an expression like A

+

B, A and B are called operands, and arithmetic operators are generally straightforward.

Comparisons, too, are straightforward with numbers. However, the rules of comparison are more complex for strings. Strings are compared on a character-by-character basis until the end of the shorter string is reached. If the characters in both strings are identical to the end of the shorter string, then the shorter one is con-sidered the lesser. Characters later in the CBM ASCII sequence are concon-sidered greater than those earlier in the series. Thus, even though the string "1" is less than the string "10", the string "5" is greater than the string "449".

Functions

Some BASIC keywords, called functions, are valid only when followed by an ex-pression in parentheses. They may be used either to the right of assignment state-ments or within expressions and will return a value dependent on the expression in paren th eses.

Numeric functions return numeric values and include SQR, LOG, and EXP.

String functions return string values and include LEFT$, MID$, RIGHT$, and CHR$.

(The last character of all string functions is a $, like that of string variable names.) PEEK, though not a function in the mathematical sense, has the syntax of a numeric function and is considered one.

Certain functions (for instance, FRE) use a so-called dummy parameter. This is an expression required by the interpreter's syntax-checking routine; however, it is ignored by the code which evaluates the function. Typically, dummy parameters are given a value of 0, as in PRINT FRE(O).

Expressions

Expressions may be numeric, string, or logical.

Numeric expressions are a valid arrangement of one or more numerals, nu-meric functions, real and integer variables, operators, or parentheses. Logical ex-pressions may also be included. Numeric exex-pressions can replace numbers in many BASIC constructions; for example, the right-hand portion of the assignment state-ment X = SQR(M)

+

PEEK(SCREEN

+

J).

String expressions are valid arrangements of one or more of: literals, string functions, string variables, the string operator +, and parentheses. String expressions can replace literals in many BASIC constructions; for example, in X$ = MID$("HI" + NAME$,l,L)+CHR$(13), which assigns X$ something like "HI BOH" with a RE-TURN character added.

Logical (or Boolean) expressions evaluate whether a relationship is true or false (-1 or 0, respectively, in VIC BASIC) and usually contain one or more relational op-erators

«,

=, or », logical operators, parentheses, numeric, or string expressions.

Their main use is in IF statements. For example, IF X$ = "Y" OR X$ = "N" GOTO 100 is a logical expression.

VIC BASIC doesn't draw sharp distinctions between logical and arithmetic ex-pressions; they are evaluated together and can be mixed. This allows constructions like IF INT(YRj4)*4=YR AND MN=2 THEN PRINT "29 DAYS" or DAYS = 31 + 2*(M=2) + (M=4 OR M=6 OR M=9 OR M=l1), where the value -1 generated by a true statement is used in the calculation of days in a month. Such expressions may evaluate differently on other machines (Apple has true = 1), so purists should avoid them.

Another aspect of logical expressions is that it's easy to program them in-correctly. Mistyping may be undetected by BASIC (for instance, if AND is typed aND, the example above interprets the expression as IF INT(YRj4)*4 THEN PRINT

"29 DAYS" which is a valid expression but gives the wrong result). In addition, logi-cal expressions have low priority. That means that they're executed last and can have wide but unexpected influence. For example, the expression IF PEEK(X)=O AND PEEK(X + 1)=0 THEN END looks for two zero bytes, but IF PEEK(X) AND PEEK(X+1)=0 ends whenever PEEK(X) is nonzero and PEEK(X+1)=O.

"True" and "false" are actually two-byte expressions like integer variables. A value of -1 means all bits are 1; a value of 0 (false) means all bits are O. Chapter 5 explains this in greater detail.

Evaluation

Every intermediate result in an expression must be valid. Numerals must be in the floating point range, strings must be no longer than 255 characters, and logical ex-pressions must be within the same integer range.

Statements

A statement is a syntactically correct portion of BASIC separated from other state-ments by an end-of-line marker or a colon. All statestate-ments begin with a BASIC keyword or (where LET has been omitted) with a variable. There are several types of statements; they are listed below.

Assignment statement (LET variable = expression). LET is optional, but its presence often makes the intention much clearer. Languages like Pascal indicate assignments with the symbol ":=", read as "becomes."

Conditional statement (IF [logical expression] THEN [statement]). These statements evaluate the IF expression; if it is true, they invoke the THEN expression.

Statements altering flow of control. These include GOTO, GOSUB, RETURN, and STOP.

Input statement. Such statements get data from a device or a DATA statement;

examples are INPUT, GET, INPUT#, GET#, and READ.

Statements governing repetition of blocks of code. For example, FOR-NEXT loops.

BASIC Reference Guide

Output statements. These statements (PRINT, PRINT#) send data to screen, disk, cassette, or some other device.

REM statements. REM statements allow the programmer to include comments for documentation. The interpreter detects the REM statement and ignores the remainder of the line when the program runs. Program lines which are included for information or as markers, but which never run, can be included in this category.

Type conversion statement. Converts between string variables and literals, real variables and numerals, or integers and numerals, using such functions as ASC, CHR$, INT, STR$, VAL.

Dans le document the Programming (Page 23-27)