OPTION BASE [0 | 1]
10 OPTION BASE 0 DIM name$(4) 20 FOR i = 0 TO 4 INPUT 'Enter a name': name$(i) PRINT i; ' Hello, '; name$(i) NEXT i 30 END RNH Enter a name? June 0 Hello, June Enter a name? Tony 1 Hello, Tony Enter a name? Sandy 2 Hello, Sandy Enter a name? Carl 3 Hello, Carl Enter a name? Liz 4 Hello, Liz
Use OPTION BASE to set the default low bound for arrays to suit your needs. You have the option of starting the array with element O or element 1.
When no low bound is specified for a dimension, the default is 1. The OPTION BASE statement lets you specify a default low bound of 0 or 1. When any following DIM or REDIM statements are executed, INTOUCH defaults the low bound to 0 or 1 as specified.
A LOOP is a section of code that can be repeated. There are two types of loops.
A FOR loop is used when you need to repeat a block of code a specific number of times. FOR loops are also good when you need to know how many times the loop was executed. You might use a FOR loop if you need to input 10 similar data items. You might use a FOR loop as a counter--say to count from 1 to 1000, or if you need to make calculations from each of the 10 data items entered.
DO loops are used when you need to execute a block of code until a specific condition is met. For instance, you might use a DO loop if you need to enter numbers until a 0 is entered. Or you might use DO loops if you need to do calculations until two numbers match, or if you need to continue a process until either the user chooses to stop or until a final result is reached.
Loops are constructs. They are created by using several statements which can only be used in conjunction with one another (FOR/NEXT, DO/LOOP). The statements which make up the constructs are described together.
FOR index_var = num_expr1 TO num_expr2 [STEP num_expr3] --- --- block of code --- NEXT index_var
10 DIM name$(4) 20 FOR j = 1 TO 4 INPUT 'Enter name': name$(j) PRINT j; ' '; name$(j) NEXT j 30 PRINT 'Finished' PRINT 'Final value:'; j 40 END RNH Enter name? Jack 1 Jack Enter name? Tom 2 Tom Enter name? Sue 3 Sue Enter name? Toby 4 Toby Finished Final value: 5
Use the FOR loop to execute a block of code a specific number of times. You can use this construct to repeat a section of code a certain number of times.
In the above example, the INPUT and PRINT statements make up the body of the loop. This block of code is executed each time the loop is repeated. (For clarity, the body of the loop is indented two spaces.) The FOR statement marks the beginning of the loop and determines how many times the loop is repeated. The NEXT statement marks the end of the loop.
index variable | V FOR J = 1 TO 4 <-- limit expression ^ | initial expression
General Information
The index variable keeps track of how many times the loop has been executed. The initial expression is the number INTOUCH begins counting at. The limit expression is the number INTOUCH stops counting at. In the example, INTOUCH counts from 1 to 4, so the loop executes four times.
When INTOUCH runs the example program, it executes the loop four times. The first time the FOR statement is executed, the variable J is set to 1. INTOUCH executes the body of the loop. Since J = 1, INTOUCH inputs NAME$(1), Jack, and prints NAME$(J).
RNH J = 1 Enter name? Jack 1 Jack
When INTOUCH reaches the NEXT J, it adds one to J and jumps back to the beginning of the loop. J is now 2. INTOUCH checks to see if J is greater than 4. Since J isn't greater than 4, INTOUCH repeats the loop. When INTOUCH executes the loop for the last time, it jumps back to the beginning of the loop and checks to see if J is greater than 4. Since J is greater than 4, INTOUCH jumps to the statement following the NEXT J (PRINT 'Finished') and continues normal program execution.
RNH J = 1 Enter name? Jack 1 Jack J = 2 Enter name? Tom 2 Tom J = 3 Enter name? Sue 3 Sue J = 4 Enter name? Toby 4 Toby Finished Final value: 5
By default, when a FOR loop is executed, INTOUCH increments the index variable by one. The increment can be changed with the STEP option. The format of the FOR statement with the STEP option is:
FOR index_var = num_expr1 TO num_expr2 STEP num_expr3 --- --- block of code --- NEXT index_var
num_expr3 is a a numeric expression specifying the increment. Each time the FOR statement is executed, INTOUCH adds the increment to the index variable. INTOUCH stops executing the loop when the index variable is greater than the limit.
10 DIM name$(4) 20 FOR j = 1 TO 4 STEP 3 INPUT 'Enter name': name$(j) PRINT j; ' '; name$(j) NEXT j 30 PRINT 'Finished' PRINT 'Final value:'; j 40 END RNH Enter name? Fred 1 Fred Enter name? John 4 John Finished Final value: 7
FOR loops can be nested. A nested loop is a loop which begins and ends inside of another loop. Loops cannot overlap. The inner loop must begin and end completely within the outer loop.
start of 10 DIM name$(4) outer loop --- 20 FOR j = 1 TO 4 INPUT name$(j) FOR k = 1 TO j inner loop --- PRINT name$(k); ' '; NEXT k end of PRINT outer loop --- NEXT j 30 PRINT 'Finished' 40 END RNH ? FRED FRED ? JOHN FRED JOHN ? MARY FRED JOHN MARY ? KATE FRED JOHN MARY KATE Finished
EXIT FOR
10 FOR i = 1 TO 5 INPUT 'Your name, please': name$ IF _EXIT THEN EXIT FOR PRINT 'Hello, '; name$ NEXT i 20 PRINT 'Finished' 30 END RNH Your name, please? James Hello, James Your name, please? Marian Hello, Marian Your name, please? exit
Use EXIT FOR to exit from a FOR loop.
When INTOUCH executes an EXIT FOR statement, it jumps to the first statement following the matching NEXT statement. EXIT FOR can only be used within FOR loops. If EXIT FOR is used within a nested loop, INTOUCH exits the innermost loop.
REPEAT FOR
10 FOR i = 1 TO 3 PRINT i INPUT 'Your name, please': name$ IF name$ = '' THEN REPEAT FOR PRINT 'Hello, '; name$ NEXT i 20 END RNH 1 Your name, please? George Hello, George 2 Your name, please? 2 Your name, please? Sam Hello, Sam 3 Your name, please? Tom Hello, Tom
To repeat a FOR loop without incrementing the index variable.
REPEAT FOR repeats all or part of the body of a loop. REPEAT FOR can only be used in FOR loops. When INTOUCH executes REPEAT FOR, it jumps to the first statement following the FOR statement.
If REPEAT FOR is used within a nested loop, INTOUCH repeats the innermost loop.
10 FOR i = 1 TO 10 FOR j = 1 to 5 INTOUCH will PRINT j repeat this INPUT 'Your name, please': name$ inner loop -------- IF name$ = '' THEN REPEAT FOR PRINT 'Hello, '; name$ NEXT j PRINT 'We now have'; i; 'set(s) of names.' NEXT i 20 END
ITERATE FOR
10 FOR i = 1 TO 3 PRINT i INPUT 'Your name, please' : name$ IF name$ = 'SKIP' THEN ITERATE FOR PRINT 'Hello, '; name$ NEXT i 20 END RNH 1 Your name, please? Toby Hello, Toby 2 Your name, please? SKIP 3 Your name, please? Sam Hello, Sam
To skip code processing.
When INTOUCH executes ITERATE FOR, it jumps to the NEXT statement. Any statements between the ITERATE FOR and the NEXT statement will be skipped.
If ITERATE FOR is used in a nested loop, INTOUCH iterates the innermost loop.
10 FOR i = 1 TO 10 FOR j = 1 TO 5 PRINT j INPUT 'Your name, please': name$ INTOUCH will IF name$ = 'SKIP' THEN ITERATE FOR skip this line ----- PRINT 'Hello, '; name$ NEXT j PRINT 'We now have'; i; 'sets of name(s).' NEXT i 20 END
DO [WHILE expr | UNTIL expr] --- --- block of code --- LOOP [WHILE expr | UNTIL expr]
10 a = 3 20 DO UNTIL a = 0 INPUT 'Your name, please': name$ PRINT 'Hello, '; name$ a = a - 1 LOOP 30 END RNH Your name, please? Sam Hello, Sam Your name, please? Sue Hello, Sue Your name, please? Bart Hello, Bart
Use a DO LOOP to execute a block of code repeatedly -- until a specified condition is met.
The simplest type of DO LOOP is an infinite DO LOOP:
10 DO INPUT 'Your name, please' : name$ PRINT 'Hello, '; name$ LOOP 20 END
In the above example, the INPUT and PRINT statements make up the body of the loop. This block of code is executed each time the loop is repeated. DO begins the loop. LOOP marks the end of the loop. When INTOUCH reaches the LOOP statement it jumps back to DO and executes the loop again.
The Ctrl/C command can be used to break out of an infinite DO loop. (For a full description of Ctrl/C, see Interrupting the Program.)
DO loops can be nested. Loops cannot overlap. The inner loop must be completely within the DO and LOOP statements of the outer loop.
start of outer loop --- 10 DO a = 5 DO UNTIL a = 0 / INPUT 'Your name' : name$ inner loop PRINT 'Hello, '; name$ \ a = a - 1 LOOP end of PRINT 'Done with a loop' outer loop --- LOOP 20 END
DO loops can be made conditional with WHILE and UNTIL options. WHILE and UNTIL set up a condition. The loop is executed if the condition is met.
11.2.1 WHILE and UNTIL Options
WHILE cond_expr
10 a = 3 20 DO INPUT 'Your name, please': name$ PRINT 'Hello, '; name$ a = a - 1 LOOP WHILE a > 0 30 PRINT 'Finished' 40 END RNH Your name, please? FRED Hello, FRED Your name, please? JOHN Hello, JOHN Your name, please? KATE Hello, KATE Finished
cond_expr is a conditional expression. When INTOUCH executes the WHILE option, it evaluates the conditional expression as either TRUE (1) or FALSE (0). If the expression is TRUE, the condition is met and INTOUCH executes the loop. INTOUCH continues executing the loop until the expression becomes FALSE. When the expression becomes FALSE, the condition is not met. INTOUCH stops executing the loop and jumps to the statement following LOOP.
UNTIL cond_expr
10 a = 3 DO UNTIL a = 0 INPUT 'Your name, please': name$ PRINT 'Hello, '; name$ a = a - 1 LOOP PRINT 'Finished' 20 END RNH Your name, please? FRED Hello, FRED Your name, please? JOHN Hello, JOHN Your name, please? KATE Hello, KATE Finished
cond_expr is a conditional expression. When INTOUCH executes the UNTIL option, it evaluates the conditional expression as either TRUE (1) or FALSE (0). If the expression is FALSE, INTOUCH executes the loop. INTOUCH continues executing the loop until the expression becomes TRUE. When the expression becomes TRUE, INTOUCH stops executing the loop and jumps to the statement following LOOP.
WHILE and UNTIL can be attached to the DO and/or to the LOOP statements. Whenever INTOUCH encounters a WHILE or UNTIL clause, it checks whether to execute the loop. So, where the WHILE and UNTIL clauses are placed affects the execution of the loop.
If a WHILE or UNTIL is attached to the DO statement, INTOUCH first checks to see whether the condition is TRUE or FALSE before it executes the loop (again). In the case of a WHILE statement, if the condition is still met (i.e. is TRUE), INTOUCH executes the loop. If the condition is not met (i.e. is FALSE or is no longer TRUE), INTOUCH does not execute the loop.
In the case of an UNTIL statement, if the condition has not been met (or is still FALSE), INTOUCH executes the loop once more. If the condition has been met (i.e. is TRUE), INTOUCH does not execute the loop again.
WHILE and UNTIL options can be placed at both ends of the loop. INTOUCH evaluates each expression in turn. When it finds that one of the conditions has or has not been met (depending upon whether it is a WHILE or UNTIL clause), INTOUCH stops executing the loop. For example, when the following program runs, INTOUCH executes the loop until A equals 5 or the user enters EXIT.
10 DIM name$(4) a = 1 20 DO UNTIL a = 5 INPUT 'Your name, please' : name$(a) a = a + 1 LOOP WHILE NOT _EXIT 30 PRINT 'Finished' 40 END
EXIT DO
10 DO INPUT 'Your name, please' : name$ IF _EXIT THEN EXIT DO PRINT 'Hello, '; name$ LOOP 20 PRINT 'Finished' 30 END RNH Your name, please? Fred Hello, Fred Your name, please? exit Finished
Use EXIT DO to exit from a DO loop.
When INTOUCH executes an EXIT DO statement, it jumps to the first statement following the LOOP or END DO statement. If EXIT DO is used within a nested loop, INTOUCH exits the innermost loop.
DO...END DO is a single iteration loop. The code between DO and END DO is processed only once unless conditional code specifies exiting or repeating the DO.
REPEAT DO
10 DO INPUT 'Your name, please': name$ IF _EXIT THEN EXIT DO IF name$ = '' THEN REPEAT DO PRINT 'Hello, '; name$ LOOP 20 END RNH Your name, please? Fred Hello, Fred Your name, please? Your name, please? exit
Use REPEAT DO to repeat part of a DO loop.
REPEAT DO repeats all or part of the body of a loop. When INTOUCH executes REPEAT DO, it jumps to the first statement following the DO statement.
If REPEAT DO is used within a nested loop, INTOUCH repeats the innermost loop.
10 DO i = i + 1 DO INTOUCH will repeat INPUT 'Your name, please': name$ this inner loop -------- IF _EXIT THEN EXIT DO IF name$ = '' THEN REPEAT DO PRINT 'Hello, '; name$ LOOP PRINT 'We now have'; i; 'set(s) of names.' LOOP 20 END
ITERATE DO
10 DO INPUT 'Your name, please': name$ IF _EXIT THEN EXIT DO IF name$ = 'SKIP' THEN ITERATE DO PRINT 'Hello, '; name$ LOOP 20 END RNH Your name, please? FRED Hello, Fred Your name, please? SKIP Your name, please? EXIT
Use ITERATE DO to repeat a loop, skipping part of the loop.
ITERATE DO repeats a loop. When INTOUCH executes ITERATE DO, it jumps to the LOOP or END DO statement. Any statements between the ITERATE DO and the end of the DO block statement will be skipped.
If ITERATE DO is used in a nested loop, INTOUCH iterates the innermost loop.
10 DO LET i = i + 1 DO INTOUCH will INPUT 'Your name, please' : name$ iterate this inner loop ---- IF name$ = 'SKIP' THEN ITERATE DO IF _EXIT THEN EXIT DO PRINT 'Hello, '; name$ LOOP PRINT 'We now have'; i; 'set(s) of names.' LOOP 20 END
EXECUTE str_expr
10 INPUT 'Enter a video attribute': video$ 20 z$ = 'print ' + video$ + & ': "This will be printed using ' + video$ + '"' 30 EXECUTE z$ 40 END RNH Enter a video attribute? bold This will be printed using bold
10 nbr_fields = 5 20 DIM check$(nbr_fields) 30 check$(1) = & 'DO \' & + ' IF len(ans$) <> 9 THEN \' & + ' MESSAGE ERROR : "SSN must be 9 digits" \' & + ' EXIT DO \' & + ' END IF \' & + ' IF not valid(ans$, "number") THEN \' & + ' MESSAGE ERROR : "SSN must be numeric" \' & + ' EXIT DO \' & + ' END IF \' & + ' PRINT "SSN is valid" \' & + 'END DO' 40 field_nbr = 1 50 INPUT 'SSN' : ans$ 60 EXECUTE check$(field_nbr) 70 END RNH SSN? 123456789 SSN is valid
EXECUTE allows you to incorporate new code into the program at run time. It is mostly used for generalized procedures, utilities, and tools.
A string is built which contains the INTOUCH statements to execute. Multiple INTOUCH statements are separated by either a line feed character (chr$(10) or a "\" character.
When an EXECUTE statement is encountered, INTOUCH compiles the code contained in the string and then runs that code. All program variables are available to the executed code and any variables established by the executed code are available to the rest of the program.
An executed string is only compiled once. When a string has been compiled, the code contained within that string is processed as efficiently as the main program code.
The EXECUTE statement makes the coding of powerful generalized routines very easy.
Conditionals are constructs which allow you to execute specific blocks of code depending on one or more conditions. For instance, suppose you are doing a tax program. You need to use the EZ form if certain conditions are met, the short form if others are met and the long form otherwise. You can use a conditional to determine which form to use.
There are two types of conditionals, IF and SELECT. The IF construct is useful when you need to check one or more conditions and execute a different block of code depending on the result. For instance, say that you need to print one statement if the user is male and under 20, another if the user is male and between 20 and 40, and still another if the user is male and over 40. The IF construct works well in this kind of situation.
The SELECT CASE construct is useful when you need to compare one main expression with several values and execute a different block of code for each possible match. For instance, suppose that in the tax program we mentioned before, you need to execute a different block of code depending on the tax bracket the user is in. SELECT CASE would let you compare a main expression---BRACKET with all the possible tax brackets (10000-15000, 15000-25000, etc.).