INTOUCH® 4GL
A Guide to the INTOUCH Language


Previous Contents Index


Chapter 11
Loops, Conditionals, Chaining and Branching

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.

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 number of times. You can use this construct to repeat a section of code a certain number of times.

DESCRIPTION:

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 

11.1.1 The STEP Option

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 

11.1.2 Nesting Loops

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 
 

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:

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.

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

11.1.5 ITERATE FOR

FORMAT:


        ITERATE FOR 

EXAMPLE:


        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 

PURPOSE:

To skip code processing.

DESCRIPTION:

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 

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:

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

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:

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.

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:

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.

Placement of WHILE and UNTIL

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.

Creating Two Conditions

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 

11.2.2 EXIT DO

FORMAT:


        EXIT DO 

EXAMPLE:


        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 

PURPOSE:

Use EXIT DO to exit from a DO loop.

DESCRIPTION:

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.

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:

Use REPEAT DO to repeat part of a DO loop.

DESCRIPTION:

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 

11.2.4 ITERATE DO

FORMAT:


        ITERATE DO 

EXAMPLE:


        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 

PURPOSE:

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

DESCRIPTION:

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 


Previous Next Contents Index