The text of a manual · part 11 of 26
Videx BASIC for DuraTrax, LaserLite, LaserLite Pro, and LaserLite Mx Data Collectors, pages 101 to 110
Pages 101 to 110 of 252
The original document is the PDF (765 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the Videx DuraTrax, Videx LaserLite, Videx LaserLite Mx and Videx LaserLite Pro
Page 101
Nested Loops You can nest FOR...NEXT loops; in other words, you can place a FOR...NEXT loop within another FOR...NEXT loop. When loops are nested, each loop must have a unique variable name as its counter. The NEXT statement for the inside loop must appear before the NEXT statement for the outside loop. The following construction is the correct form: FOR I = 1 TO 10 FOR J = 1 TO 10 FOR K = 1 TO 10 . . . NEXT K NEXT J NEXT I
The EXIT FOR provides a convenient alternative exit to FOR...NEXT loops. See the EXIT statement for more information.
Chapter 4 Statement & Function Reference
93
Page 102
GOSUB...RETURN Statement • Action Branches to, and returns from a subroutine. • Syntax GOSUB label . . .
RETURN • Parameters Argument label
Description The line label that is the first line of the subroutine.
• Remarks Care must be taken not to execute a RETURN statement without first executing a GOSUB statement. The program returns to the statement after the GOSUB statement. You may call a subroutine any number of times in a program. You may also call a subroutine from within another subroutine. How deeply you can nest subroutines is limited only by the available stack space. A subroutine may contain more than one RETURN statement. A RETURN statement in a subroutine makes BASIC branch back to the statement following the most recently executed GOSUB statement. Subroutines may appear anywhere in the program, but it is good programming practice to make them readily distinguishable from the main program. To prevent inadvertent entry into a subroutine, precede it with an END or GOTO statement that directs program control around the subroutine. 94
Chapter 4 Statement & Function Reference
Page 103
• Example 1 REM GOSUB...RETURN Statement example 1 PRINT "main code"; SLEEP 0 GOSUB Sub1 Label1: CLS PRINT "back in main code"; SLEEP 0 END Sub1: CLS PRINT "in sub 1"; SLEEP 0 GOSUB Sub2 Label2: CLS PRINT "back in sub 1"; SLEEP 0 RETURN Sub2: CLS PRINT "in sub 2"; SLEEP 0 RETURN
Output: main code in sub 1 in sub 2 back in sub 1 back in main code
Chapter 4 Statement & Function Reference
95
Page 104
• Example 2 REM GOSUB...RETURN Statement example 2 DIM m% CONST M1$ = "UP for Help" CONST M2$ = "DOWN to Quit" PRINT M1$ PRINT M2$; DO GOSUB getMenu LOOP END getMenu: m% = 0 WHILE m% = 0 m% = ASC(INKEY$()) WEND CLS IF m% = 1 THEN rem scan button PRINT " Wrong " PRINT " Button! "; BEEP BEEP ELSEIF m% = 2 THEN rem up button PRINT "Help"; ELSEIF m% = 4 THEN rem down button PRINT "Press Again" PRINT "to clear screen"; SLEEP 5 END ENDIF SLEEP 3 CLS PRINT M1$ PRINT M2$; RETURN
96
Chapter 4 Statement & Function Reference
Page 105
GOTO Statement • Action Branches unconditionally to the specified line. • Syntax GOTO label$ • Parameters Argument label$
Description Line label to branch to.
• Remarks The GOTO statement provides a way to branch unconditionally to another line (label$). It is good programming practice to use structured control statements (DO...LOOP, FOR...NEXT, IF...THEN...ELSE) instead of GOTO statements because a program with many GOTO statements is difficult to read and debug.
Chapter 4 Statement & Function Reference
97
Page 106
• Example rem GOTO Statement: This is a good example of how a control rem structure, DO...LOOP, could be used instead of a GOTO. DIM s%, k%, a% k% = 0 s% = 0 a% = 0 PRINT "Press SCAN." Start: DO k% = ASC(INKEY$()) IF k% > 0 THEN EXIT DO LOOP IF k% = 1 THEN BEEP END ELSEIF k% = 2 THEN rem up arrow increases s% = s% + 5 ELSEIF k% = 4 THEN rem down arrow decreases IF s% > 5 THEN s% = s% - 5 ENDIF ENDIF a% = s% * s% rem This example calculates rem the area of a square.
CLS PRINT "Sides = ";s% PRINT "Area = "; a%; SLEEP 0 GOTO Start
98
Chapter 4 Statement & Function Reference
Page 107
HEX$ Function • Action Produces a string of hexadecimal digits to represent a one-byte or twobyte integer value. (Videx BASIC) (Note: This function is not supported by the Macintosh version of the Videx BASIC compiler.) • Syntax result$ = HEX$ ( value%, num_digits% ) • Parameters Argument value% num_digits%
Description The integer value to be converted to hexadecimal. Number of characters to convert (0 to 4). If zero is specified, only the number of digits necessary to represent the value without leading zeros will be converted.
• Remarks This function works similarly to the CHR$ function but produces a string of hexadecimal digits (containing twice as many characters) that can represent binary numbers. The characters will always be counted from the least-significant nibble (4 bits) of value%. The more significant characters are leftmost in the string produced. For example, if value% is equal to 2748 (0ABC hex) the resulting strings for all the values of num_digits% are: num_digits% result$
0 1 2 3 4
ABC C BC ABC 0ABC
Chapter 4 Statement & Function Reference
99
Page 108
The compiler flags any out-of-range values for num_digits%. If the parameter is supplied by a variable, any out-of-range values are forced into range by the formula: good
=
( (bad - 1) MOD 4 ) + 1
For example, a value of 5 becomes 1 (see Example 1) and a value of 19 becomes 3 (see Example 2). •= Example 1 good = ( (5 - 1) MOD 4) + 1 good = (4 MOD 4) + 1 good = (0) + 1 good = 1 •= Example 2 good = ( (19 - 1) MOD 4) + 1 good = (18 MOD 4) + 1 good = (2) + 1 good = 3 Note: MOD arithmetic provides the remainder of an integer division, rather than the quotient. For example: 16 MOD 5 = 1: 16 ÷ 5 = 3 with a remainder 1; 3250 MOD 256 = 178: 3250 ÷ 256 = 12 with a remainder of 178. See page 18 for complete information on MOD arithmetic. Strings in Videx BASIC are null-terminated; that is, the character with the value zero has the special function of designating the end of the string. Because of that special designation, there is no way to include a character with the value zero within the string. In working with some types of data, for example, information stored in Touch Memory buttons, it is sometimes desirable to be able to handle strings of bytes that may contain binary information. The occurrence of bytes with a zero value is likely, so trying to use normal Videx BASIC strings for manipulating binary data would cause the data to be truncated at the first occurrence of a zero byte. Converting binary values to pairs of hexadecimal digits provides a way of transmitting binary information within a string. See also BIN and TOUCH. 100
Chapter 4 Statement & Function Reference
Page 109
• Returns Returns a string that is the hexadecimal representation of value% with a specified number of characters. • Example REM BIN()and HEX$ Functions DIM hex_data$ * 4 DIM i%, number% FOR i% = 1 to 10 number% = i% * 50 hex_data$ = HEX$ (number%, 0) 'convert to hex CLS PRINT str$ (number%); " in hex:" PRINT hex_data$; 'display result SLEEP 0 number% = bin (hex_data$, 0) 'convert back to decimal CLS PRINT hex_data$; " as decimal:" PRINT str$ (number%); 'display result SLEEP 0 NEXT i% END
Chapter 4 Statement & Function Reference
101
Page 110
IF...ELSEIF...ELSE...ENDIF Statement • Action Allows conditional execution and branching. • Syntax 1 (single line) IF Booleanexpression% THEN thenpart • Syntax 2 (block) IF Booleanexpression1% THEN [statementblock-1] [ELSEIF Booleanexpression2% THEN [statementblock-2]] . . . [ELSE [statementblock-n]] ENDIF • Parameters 1 (single line) The following list describes the parts of the single-line form: Argument Booleanexpression% thenpart
Description Any expression that evaluates to true (nonzero) or false (zero). The statements or branches performed when Booleanexpression% is true (thenpart). The syntax is described below.
The thenpart has the following syntax: { statements | GOTO linelabel }
102
Chapter 4 Statement & Function Reference