M!INTOUCH® 4GL Y

INTOUCH® 4GL
A Guide to the INTOUCH Language

A
Previous page... 6Table of Contents
8

10.1.3 OPTION BASE



FORMAT:



        OPTION BASE [0 | 1] 


EXAMPLE:



        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 


PURPOSE:





BUse OPTION BASE to set the default low bound for arrays to Ksuit your needs. You have the option of starting the array with element O or element 1. 



DESCRIPTION:





GWhen no low bound is specified for a dimension, the default is 1. The GOPTION BASE statement lets you specify a default low bound of 0 or 1. JWhen any following DIM or REDIM statements are executed, INTOUCH defaults &the low bound to 0 or 1 as specified. 




\

Chapter 11
Loops, Conditionals, Chaining and Branching



GA 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 Aspecific number of times. FOR loops are also good when you need Cto know how many times the loop was executed. You might use a FOR Bloop if you need to input 10 similar data items. You might use a CFOR 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. 

ADO loops are used when you need to execute a block of code until Da specific condition is met. For instance, you might use a DO loop Eif you need to enter numbers until a 0 is entered. Or you might use DDO loops if you need to do calculations until two numbers match, or Cif you need to continue a process until either the user chooses to )stop or until a final result is reached. 

DLoops are constructs. They are created by using several statements Bwhich can only be used in conjunction with one another (FOR/NEXT, EDO/LOOP). The statements which make up the constructs are described together. 

6

11.1 FOR/NEXT Loop



FORMAT:



@        FOR index_var = num_expr1 TO num_expr2 [STEP num_expr3] .                ---                           #                ---  block of code                 ---         NEXT index_var 


EXAMPLE:



        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 


PURPOSE:





>Use the FOR loop to execute a block of code a specific Knumber of times. You can use this construct to repeat a section of code a certain number of times. 



DESCRIPTION:





IIn the above example, the INPUT and PRINT statements make up the body of Jthe loop. This block of code is executed each time the loop is repeated. A(For clarity, the body of the loop is indented two spaces.) The 9FOR statement marks the beginning of the loop and Adetermines how many times the loop is repeated. The NEXT %statement marks the end of the loop. 

         index variable                  |                  V 6             FOR J = 1 TO 4   <-- limit expression                      ^                      |               initial expression 



General Information









AThe 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 Inumber INTOUCH stops counting at. In the example, INTOUCH counts from 1 'to 4, so the loop executes four times. 

HWhen INTOUCH runs the example program, it executes the loop four times. JThe first time the FOR statement is executed, the variable J is set to 1. NINTOUCH 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 


HWhen INTOUCH reaches the NEXT J, it adds one to J and jumps back to the Kbeginning of the loop. J is now 2. INTOUCH checks to see if J is greater Gthan 4. Since J isn't greater than 4, INTOUCH repeats the loop. When LINTOUCH 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 Cis greater than 4, INTOUCH jumps to the statement following Fthe 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 
<

11.1.1 The STEP Option



FBy default, when a FOR loop is executed, INTOUCH increments the index Jvariable by one. The increment can be changed with the STEP option. The 5format 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 


Fnum_expr3 is a a numeric expression specifying the increment. KEach time the FOR statement is executed, INTOUCH adds the increment to the Jindex 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 
:

11.1.2 Nesting Loops



HFOR loops can be nested. A nested loop is a loop which begins and ends Ainside 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 9                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  
5

11.1.3 EXIT FOR



FORMAT:



        EXIT FOR 


EXAMPLE:



        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 


PURPOSE:





-Use EXIT FOR to exit from a FOR loop. 



DESCRIPTION:





MWhen INTOUCH executes an EXIT FOR statement, it jumps to the first statement Mfollowing the matching NEXT statement. EXIT FOR can only be used within FOR Eloops. If EXIT FOR is used within a nested loop, INTOUCH exits the innermost loop. 



7

11.1.4 REPEAT FOR



FORMAT:



        REPEAT FOR 


EXAMPLE:



        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 


PURPOSE:





>To repeat a FOR loop without incrementing the index variable. 



DESCRIPTION:





=REPEAT FOR repeats all or part of the body of a loop. MREPEAT 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 3                            PRINT 'Hello, '; name$ !                          NEXT j E                          PRINT 'We now have'; i; 'set(s) of names.'                         NEXT i                     20  END 

8

11.1.5 ITERATE FOR



FORMAT:



        ITERATE FOR 


EXAMPLE:



        10  FOR i = 1 TO 3               PRINT i 0              INPUT 'Your name, please' : name$ 4              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 


PURPOSE:





To skip code processing. 



DESCRIPTION:





?When INTOUCH executes ITERATE FOR, it jumps to the NEXT Jstatement. Any statements between the ITERATE FOR and the NEXT statement will be skipped. 

NIf 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 1skip this line -----      PRINT 'Hello, '; name$                         NEXT j C                        PRINT 'We now have'; i; 'sets of name(s).'                       NEXT i                   20  END 

0

11.2 DO LOOP



FORMAT:



%        DO [WHILE expr | UNTIL expr]                 --- #                ---  block of code                 --- '        LOOP [WHILE expr | UNTIL expr] 


EXAMPLE:



        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 


PURPOSE:





>Use a DO LOOP to execute a block of code repeatedly -- $until a specified condition is met. 



DESCRIPTION:





5The simplest type of DO LOOP is an infinite DO LOOP: 

        10  DO 0              INPUT 'Your name, please' : name$ %              PRINT 'Hello, '; name$             LOOP         20  END 


IIn the above example, the INPUT and PRINT statements make up the body of Jthe loop. This block of code is executed each time the loop is repeated. 6DO begins the loop. LOOP marks the end Jof the loop. When INTOUCH reaches the LOOP statement it jumps back to DO and executes the loop again. 

OThe Ctrl/C command can be used to break out of an infinite DO loop. 2(For a full description of Ctrl/C, see Interrupting the Program.) 

ADO loops can be nested. Loops cannot overlap. The inner Mloop 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$ 7               inner loop       PRINT 'Hello, '; name$ *                           \    a = a - 1 #                              LOOP 7           end of             PRINT 'Done with a loop' !           outer loop ---   LOOP                          20  END 


@DO loops can be made conditional with WHILE and UNTIL options. AWHILE and UNTIL set up a condition. The loop is executed if the condition is met. 



D

11.2.1 WHILE and UNTIL Options



FORMAT:



        WHILE cond_expr 


EXAMPLE:



        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 


DESCRIPTION:





Econd_expr is a conditional expression. When INTOUCH executes Athe WHILE option, it evaluates the conditional expression 7as either TRUE (1) or FALSE (0). If the Hexpression is TRUE, the condition is met and INTOUCH executes the loop. IINTOUCH continues executing the loop until the expression becomes FALSE. LWhen the expression becomes FALSE, the condition is not met. INTOUCH stops >executing the loop and jumps to the statement following LOOP. 





FORMAT:



        UNTIL cond_expr 


EXAMPLE:



        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 


DESCRIPTION:





Econd_expr is a conditional expression. When INTOUCH executes Athe UNTIL option, it evaluates the conditional expression 7as either TRUE (1) or FALSE (0). If the Mexpression is FALSE, INTOUCH executes the loop. INTOUCH continues executing Ithe loop until the expression becomes TRUE. When the expression becomes LTRUE, INTOUCH stops executing the loop and jumps to the statement following LOOP. 



%

Placement of WHILE and UNTIL



JWHILE and UNTIL can be attached to the DO and/or to the LOOP statements. JWhenever INTOUCH encounters a WHILE or UNTIL clause, it checks whether to Dexecute the loop. So, where the WHILE and UNTIL clauses are placed $affects the execution of the loop. 

JIf a WHILE or UNTIL is attached to the DO statement, INTOUCH first checks Jto see whether the condition is TRUE or FALSE before it executes the loop J(again). In the case of a WHILE statement, if the condition is still met H(i.e. is TRUE), INTOUCH executes the loop. If the condition is not met I(i.e. is FALSE or is no longer TRUE), INTOUCH does not execute the loop. 

EIn the case of an UNTIL statement, if the condition has not been met B(or is still FALSE), INTOUCH executes the loop once more. If the Dcondition has been met (i.e. is TRUE), INTOUCH does not execute the loop again. 

Creating Two Conditions



AWHILE and UNTIL options can be placed at both ends of the loop. JINTOUCH evaluates each expression in turn. When it finds that one of the Iconditions has or has not been met (depending upon whether it is a WHILE Gor UNTIL clause), INTOUCH stops executing the loop. For example, when Gthe 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 3              INPUT 'Your name, please' : name$(a)               a = a + 1 !            LOOP WHILE NOT _EXIT         30  PRINT 'Finished'         40  END 
4

11.2.2 EXIT DO



FORMAT:



        EXIT DO 


EXAMPLE:



        10  DO 0              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 


PURPOSE:





+Use EXIT DO to exit from a DO loop. 



DESCRIPTION:





BWhen INTOUCH executes an EXIT DO statement, it jumps to the first Astatement following the LOOP or END DO statement. If EXIT DO is =used within a nested loop, INTOUCH exits the innermost loop. 

ADO...END DO is a single iteration loop. The code between GDO and END DO is processed only once unless conditional code specifies exiting or repeating the DO. 



6

11.2.3 REPEAT DO



FORMAT:



        REPEAT DO 


EXAMPLE:



        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 


PURPOSE:





2Use REPEAT DO to repeat part of a DO loop. 



DESCRIPTION:





CREPEAT DO repeats all or part of the body of a loop. When INTOUCH Bexecutes 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. 

3                          10  DO                   *                                i = i + 1 *                                DO        C        INTOUCH will repeat       INPUT 'Your name, please': name$ ;        this inner loop --------  IF  _EXIT  THEN  EXIT DO B                                  IF  name$ = ''  THEN  REPEAT DO 9                                  PRINT 'Hello, '; name$ %                                LOOP K                                PRINT 'We now have'; i; 'set(s) of names.' #                              LOOP "                          20  END 

7

11.2.4 ITERATE DO



FORMAT:



        ITERATE DO 


EXAMPLE:



        10  DO /              INPUT 'Your name, please': name$ '              IF  _EXIT  THEN  EXIT DO 3              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 


PURPOSE:





CUse ITERATE DO to repeat a loop, skipping part of the loop. 



DESCRIPTION:





JITERATE DO repeats a loop. When INTOUCH executes ITERATE DO, it jumps to Ithe LOOP or END DO statement. Any statements between the ITERATE DO and 3the end of the DO block statement will be skipped. 

GIf ITERATE DO is used in a nested loop, INTOUCH iterates the innermost loop. 

%                              10  DO 2                                    LET i = i + 1 '                                    DO H        INTOUCH will                  INPUT 'Your name, please' : name$ K        iterate this inner loop ----  IF  name$ = 'SKIP'  THEN  ITERATE DO ?                                      IF  _EXIT  THEN  EXIT DO =                                      PRINT 'Hello, '; name$ )                                    LOOP O                                    PRINT 'We now have'; i; 'set(s) of names.' '                                  LOOP &                              20  END 

0

11.3 EXECUTE



FORMAT:



        EXECUTE str_expr 


EXAMPLE:



4        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


EXAMPLE:



        10  nbr_fields = 5 #        20  DIM check$(nbr_fields)         30  check$(1) = &               'DO \' & ;              + '  IF  len(ans$) <> 9  THEN \' & E              + '    MESSAGE ERROR : "SSN must be 9 digits" \' & &              + '    EXIT DO \' & #              + '  END IF \' & @              + '  IF  not valid(ans$, "number") THEN  \' & D              + '    MESSAGE ERROR : "SSN must be numeric" \' & &              + '    EXIT DO \' & #              + '  END IF \' & 1              + '  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 


DESCRIPTION:





;EXECUTE allows you to incorporate new code into the Dprogram at run time. It is mostly used for generalized procedures, utilities, and tools. 

DA 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. 

DWhen 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 Bestablished by the executed code are available to the rest of the program. 

BAn 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. 



5

11.4 Conditionals



@Conditionals are constructs which allow you to execute specific Cblocks of code depending on one or more conditions. For instance, Esuppose you are doing a tax program. You need to use the EZ form if Ecertain conditions are met, the short form if others are met and the Clong form otherwise. You can use a conditional to determine which form to use. 

FThere are two types of conditionals, IF and SELECT. The IF construct Fis useful when you need to check one or more conditions and execute a Ddifferent block of code depending on the result. For instance, say Cthat you need to print one statement if the user is male and under A20, another if the user is male and between 20 and 40, and still Aanother if the user is male and over 40. The IF construct works well in this kind of situation. 

AThe SELECT CASE construct is useful when you need to compare one Bmain expression with several values and execute a different block Dof code for each possible match. For instance, suppose that in the Gtax program we mentioned before, you need to execute a different block Bof code depending on the tax bracket the user is in. SELECT CASE ?would let you compare a main expression---BRACKET with all the 8possible tax brackets (10000-15000, 15000-25000, etc.). 



4


Next page... | 6Table of Contents