• Aucun résultat trouvé

LINE NUMBERS VERSUS LABELS

Dans le document Interactive BASIC Compiler (Page 150-155)

STRUCTURED PROGRAMMING

LINE NUMBERS VERSUS LABELS

The standard line editor (command mode) uses line number for three reasons:

1. Remain compatible with older versions of BASIC 2. For the Standard line editor commands

3. To give more easily understandable error messages

To make programs easier to read you should use alphanumeric labels for subroutines or any other area of a program that does a specific function.

It is much easier to follow the flow of a program if GOSUB, GOTO and other branching statements use labels instead of line numbers.

To LIST programs without line numbers use LIST +. Many versions of ZBasic now use full screen editors that don't require line numbers. See your appendix for specifics.

Structure

148

STRUCTURED PROGRAMMING

INDENTATION OF LOOPS, LONG FN and LONG IF

Some versions of structured languages require that you manually indent nested statements for readability.

ZBaslc does all the Indenting automatically!

Each nested portion of a program will be indented 2 spaces when the program is listed. Program statements like FOR-NEXT, WHILE-WEND, DO-UNTIL, LONG FN, LONG-IF etc. will be indented.

Example using LIST +:

LONG FN KillFile(file$)

PRINT@(0,10);"Erase ";file$;" YIN";

DO

MULTIPLE LINE STATEMENTS

149

Structure

ZBasic allows putting more than one statement on a line with ":" (colon). While this is handy for many reasons, over-use of this capability can make a program line very difficult to understand.

UNSTRUCTURED 10*FORX=lT0100:DO:G=G+1:PRINT G:UNTILG=99:NEXT STRUCTURED FOR X = 1 TO 100

'Note: An asterisk will appear at the beginning of a line containing a complete loop if that line is not already indented. In that case the line will be un-indented two spaces (as in the examples above).

STRUCTURED PROGRAMMING

SPACES BETWEEN WORDS

To make code more readable, you should insert spaces between words, variables and commands, just as you do when writing in English. While ZBasic does not care if spaces are used (unless you configure ZBasic to require spaces), ~ is a good practice to insert spaces at appropriate places to make reading the program easier.

Hard to Read Easier to Read

VARIABLE NAMES

IFX=93*24THENGOSUB"SUB56"ELSEEND IF X=93*24 THEN GOSUB "SUBS6" ELSE END

To make code more readable, use logical words for variables.

Hard to Read Easier to Read

B=OP+I

Balance = Old_Principle + Interest

ZBasic allows variable name lengths up to the length of a line, but only the first 15 characters in the name are significant. Do not use spaces or symbols to separate words in a name, use underlines; Buildin(LPrinciple, Freds_House.

Keywords may not be used in variable names unless they are in lowercase and

"Convert to Uppercase" is "NO" (this is the default). Also see next paragraph.

INCLUDING KEYWORDS IN VARIABLES

To allow keyword in variables configure ZBasic for; "Spaces Required after Keywords" (not available on all systems). See "Configure".

HOW CASE AFFECTS VARIABLE NAMES

To make the variable "FRED" and "fred" the same variable configure ZBasic for

"Convert to Uppercase". See "Configure".

GLOBAL VERSUS LOCAL VARIABLES

Programmers familiar with LOCAL variables in PASCAL and some other languages can structure their variable names to approximate this in ZBasic. (All ZBasic variables are global.)

GLOBAL variables should start with a cap~alletter.

LOCAL variables should start with lowercase. Many programmers also use (and re-use) variables like temp$ or local$ for local variables.

Structure

150

STRUCTURED PROGRAMMING

DEFINING FUNCTIONS

Use DEF FN or LONG FN to define functions and then call that function by name. This is easy reading for people trying to decipher your programs. It saves program space as well. FN names have the same definition as variable names. Passing values to functions in variables is also very easy.

LONG FN may be used when a function the size of a subroutine is needed.

One FN may call previously defined functions.

LOADING PREVIOUSLY CREATED SUBROUTINES

To insert subroutines you have used in previous programs, use the APPEND command. This will append (or insert) a program saved with SAVE+ (a non-line numbered program or subroutine), into the current program starting at the line number you specify; APPEND Iinenumber or label filename

Be sure to avoid the use of line numbers or GOTO statements in your subroutine to make them more easily portable.

If using variables that are to be considered LOCAL, we recommend keeping those variables all lowercase characters to differentiate them from GLOBAL variables (all ZBasic variables are GLOBAL).

Sometimes LONG FN may be more appropriate for re-usable subroutines.

LISTINGS WITHOUT LINE NUMBERS

151

Structure

To make program listings easier to read, use LIST+ or LLIST+ to list a program without line numbers.

ZBasic automatically indents nested statements with LIST for even more readability.

~&

Macintosh: Listings can be sent to the Screen, LaserWriter or ImageWriter without linenumbers and with keywords boldfaced by using LLIST+*.

MSDOS: Screen listings with highlighted keywords and no linenumbers are accomplished with LIST+* (no printer support for highlighted keywords).

LONG IF

STRUCTURED PROGRAMMING

For more control 01 the I F statement. ZBasic provides LONG IF lor improved readability and power.

UNSTRUCTURED

10 IFX=ZTHENY=10+H:G=G+Y:F=F+RELSEGOSUB122:T=T-1

STRUCTURED

LONG IF X=Z Y=10+H G=G+Y F=F+R XELSE

GOSUB"READ"

T=T-1 END IF

UNSTRUCTURED

10 FORI=-3T03:PRINT"I= ";I:IF I> THEN IF 1>-3 AND 1<3 PRINT I;">O",ELSEPRINT"Inner If False":GOTO 30 20 *PRINT I;"<=O", :X=-4:DO:X=X+1:PRINT"X=";X:UNTILX=I 30 NEXT I

STRUCTURED

FOR I = -3 TO 3: PRINT "1= ";I LONG IF I > 0

LONG IF I > -3 AND I < 3 PRINT Ii n> 0",

XELSE

PRINT "Inner LONG IF false"

END IF XELSE

PRINT Ii"<= 0", X = -4

DO : X=X+1 PRINT"X="iX UNTIL X=I ENDIF NEXT I

Important Note: Any loops enclosed in LONG IF structures must be completely contained within the LONG IF. END IF construct.

~.

The Macintosh and IBM versions also support SELECT CASE. a structured.

multi-conditional LONG IF statement. See appendices lor syntax.

Structure

152

Dans le document Interactive BASIC Compiler (Page 150-155)