INTOUCH® 4GL
A Guide to the INTOUCH Language


Previous Contents Index

11.9.1 Branching and Code Blocks

Certain constructs (such as GOSUB) include a block of code which is executed within the construct. You can transfer control within these code blocks and out of these code blocks. For example:


                           10  DIM name$(4) 
                           20  FOR i = 1 TO 4 
                                 INPUT 'Your name, please': name$(i) 
       branches within block --    IF  name$(i) <> ''  THEN  GOTO hello 
       branches out of block --    IF  _EXIT  THEN  EXIT FOR 
                                 hello: 
                                   PRINT 'Hello!' 
                           30  NEXT i 
                           40  PRINT 'Finished' 
                           50  END        

11.10 GOTO

FORMAT:


        GOTO target 

EXAMPLE:


        10  start: 
            INPUT 'Your Name': name$ 
            IF  _EXIT  THEN  GOTO the_end 
            PRINT 'Hello, '; name$ 
            GOTO start 
        20 the_end: END 
 
        RNH 
        Your Name? Tony 
        Hello, Tony 
        Your Name? Betty 
        Hello, Betty 
        Your Name? exit 

PURPOSE:

Use GOTO when you want to jump from one part of your program to another.

DESCRIPTION:

target is the line to which control is being transferred. target can be a label or line number.

Note

It is recommended that you branch to labels rather than line numbers. Should the line numbers change, the change would have no effect on your branching code.

When INTOUCH executes a GOTO statement, it branches to the target line specified. Any code between the GOTO and the target line is skipped. INTOUCH continues normal program execution at the target line.

11.10.1 ON...GOTO...[ELSE]

FORMAT:


        ON int_expr GOTO target1 [, target2...] [ELSE statement] 

EXAMPLE:


        10  ask: INPUT 'Procedure (1=add, 2=del, 3=exit)': pro 
        20  ON pro GOTO add, del, done 
            add: 
              PRINT 'Adding...' 
              GOTO ask 
            del: 
              PRINT 'Deleting...' 
              GOTO ask 
        30  done: END 
 
        RNH 
        Procedure (1=add, 2=del, 3=exit)? 2 
        Deleting... 
        Procedure (1=add, 2=del, 3=exit)? 1 
        Adding... 
        Procedure (1=add, 2=del, 3=exit)? 3 

PURPOSE:

Sometimes you want to jump to one of several places in your program. ON...GOTO lets you jump to one of several locations depending on a condition you set up.

DESCRIPTION:

int_expr is the integer expression or condition to check. target1, target2 ... is a list of places to jump to. Each target can be a label or line number.

ON..GOTO branches to one of several targets depending on the value of an integer expression. The simplest version of ON..GOTO is,


        ON int_expr GOTO target1, target2... 

int_expr is an integer expression whose value determines where control is transferred. When INTOUCH executes the ON GOTO statement, it evaluates the integer expression. If the value is 1, INTOUCH branches to the first target in the list. If the value is 2, INTOUCH branches to the second target, and so on.

If the expression does not evaluate to an integer, INTOUCH rounds it. The value must be from 1 to the number of targets (unless there is an ELSE clause). For example, if there are five targets, the integer value must be from 1 to 5.

If the value is less than 1 or greater than the number of targets, and there is no ELSE clause, INTOUCH generates an exception.

Targets can be labels or line numbers.

INTOUCH supports 128 targets for ON...GOTO. More targets than 128 gives an "expression too complex" exception.

11.10.1.1 ELSE Option

The ELSE option executes a statement if the integer expression does not find a target. ON GOTO with the ELSE option looks like this:


        ON int_expr GOTO target1 [, target2...] ELSE statement 

ELSE must be followed by a statement. The ELSE is executed if the integer value exceeds the number of targets in the list, is zero or is negative.


        10  start: 
            INPUT 'Procedure (1=add, 2=del, 3=exit)': pro 
        20  ON pro GOTO add, del, done ELSE PRINT 'Enter 1, 2 or 3' 
            GOTO start 
            add: 
              PRINT 'Adding...' 
              GOTO start 
            del: 
              PRINT 'Deleting...' 
              GOTO start 
        30  done: 
            PRINT 'Finished' 
            END 
 
        RNH 
        Procedure (1=add, 2=del, 3=exit)? -2 
        Enter 1, 2 or 3 
        Procedure (1=add, 2=del, 3=exit)? 1 
        Adding... 
        Procedure (1=add, 2=del, 3=exit)? 3 
        Finished 

11.11 GOSUB/RETURN

11.11.1 GOSUB

FORMAT:


        GOSUB target 
               . 
               . 
               . 
        target 
            --- 
            ---  block of code 
            --- 
        RETURN 

EXAMPLE:


        10  GOSUB get_input 
        20  PRINT 'Hello, '; name$ 
            GOTO exit 
        30  get_input: 
            INPUT 'Enter your name': name$ 
            name$ = UCASE$(name$) 
            RETURN 
        40  exit: 
            PRINT 'Finished' 
        50  END 
 
        RNH 
        Enter your name? Julian 
        Hello, JULIAN 
        Finished 

PURPOSE:

Use GOSUB to jump to another location in your program, execute the code at that location and then return to the place you left.

DESCRIPTION:

GOSUB transfers control to a subroutine. The subroutine is a block of code. INTOUCH executes the code, then returns to the statement following the GOSUB. The target can be a label, a ROUTINE statement or a line number.

Note

It is recommended that you branch to labels rather than line numbers. Should the line numbers change, the change would have no effect on your branching code.

If the target in the GOSUB statement does not exist, an exception is generated. RETURN or END ROUTINE ends the subroutine and transfers control to the statement following the GOSUB.

You can nest 64 GOSUBs up to 64 levels deep.

Note

If your label or routine name contains an underscore (_) character (e.g. ask_name, do_add), you do NOT need to use the word "GOSUB". Under these conditions, the "GOSUB" is implied. For example: using ASK_NAME would be the same as using GOSUB ASK_NAME

11.11.2 ON...GOSUB

FORMAT:


        ON int_expr GOSUB target1 [, target2...] [ELSE statement] 

EXAMPLE:


        10  start: INPUT 'Procedure (1=add, 2=del, 3=exit)': pro 
        20  ON pro GOSUB add, del, done ELSE PRINT 'Enter 1, 2 or 3' 
            GOTO start 
            add: 
              PRINT 'Adding...' 
              RETURN 
            del: 
              PRINT 'Deleting...' 
              RETURN 
        30  done: 
            PRINT 'Finished' 
            END 
 
        RNH 
        Procedure (1=add, 2=del, 3=exit)? add 
        Non-numeric input when number expected at START 
        Procedure (1=add, 2=del, 3=exit)? 5 
        Enter 1, 2 or 3 
        Procedure (1=add, 2=del, 3=exit)? 1 
        Adding... 
        Procedure (1=add, 2=del, 3=exit)? 3 
        Finished 

PURPOSE:

Use ON..GOSUB when you want to jump to one of several subroutines depending on a condition you set up. ON..GOSUB jumps to one of the locations, executes the code at that location and then returns to the first statement after the ON..GOSUB.

DESCRIPTION:

ON..GOSUB transfers control to one of several subroutines depending on the value of an integer expression. The simplest version of ON..GOSUB is:


        ON int_expr GOSUB target1, target2... 

int_expr is an integer expression whose value determines where control is transferred. The targets begin subroutines. They can be ROUTINE statements, labels or line numbers. The subroutines are blocks of code. When INTOUCH branches to a subroutine, it executes the block of code until it reaches a RETURN or END ROUTINE statement. INTOUCH then returns to the statement following the ON..GOSUB.

When INTOUCH executes the ON GOSUB, it evaluates the integer expression. If the value is one, INTOUCH branches to the first target and executes the subroutine. If the integer is two, INTOUCH branches to the second subroutine, and so on.

int_expr must evaluate to an integer. If it does not, INTOUCH rounds the value and uses the resulting integer. The integer expression must yield an integer within the range of the target list. For example, if there are five targets in the list, the integer value must be in the range of 1 to 5. If the integer value is not in the range of the target list, and there is no ELSE clause, an exception is generated.

INTOUCH supports 128 targets for ON..GOSUB. More targets than 128 gives an "expression too complex" exception.

11.11.2.1 ELSE Option

The ELSE option executes a statement if the integer value does not yield a target. ON GOSUB with the ELSE option looks like this:


        ON int_expr GOSUB target1, target2... ELSE statement 

ELSE must be followed by a statement. The ELSE is executed if the integer value exceeds the number of targets in the list, is 0 or is negative.

The previous example shows how ELSE is used.

11.12 DISPATCH

FORMAT:


        DISPATCH str_expr 
             . 
             . 
             . 
        target 
             --- 
             ---  block of code 
             --- 
        RETURN 

EXAMPLE:


        10  INPUT 'Routine name', DEFAULT 'add': routine$ 
            DISPATCH routine$ 
            STOP 
        20  add: 
              PRINT 'Adding information...' 
            RETURN 
 
        30  change: 
              PRINT 'Changing information...' 
            RETURN 
 
        40  END 
 
        RNH 
        Routine name? add 
        Adding information... 

PURPOSE:

DISPATCH executes a routine that the program determines at run-time.

DESCRIPTION:

DISPATCH looks at the contents of the string expression (str_expr), searches for a routine with that name and GOSUBS to the routine.

str_expr is the name of the subroutine to execute.

11.13 ROUTINE/END ROUTINE

FORMAT:


        ROUTINE routine_name 
           --- 
           --- block of code 
           --- [REPEAT ROUTINE] 
           --- [EXIT ROUTINE] 
        END ROUTINE          

EXAMPLE:


        10      get_username 
        20      END 
 
        12000   ROUTINE get_username 
                  INPUT PROMPT 'Username: ': uname$ 
                  IF  _BACK  or  _EXIT  THEN  EXIT ROUTINE 
                END ROUTINE 
 
        RNH 
        Username: Tester 

DESCRIPTION:

ROUTINES are block-structured subroutines. They provide a convenient way to name a block of code. Routines can be edited as a complete unit, or partially edited by line number.

To execute a ROUTINE, use the GOSUB or DISPATCH statement or just the name of the routine. You CANNOT fall into a routine the way you can a subroutine that starts with a label.

11.13.1 EXIT ROUTINE

FORMAT:


        EXIT ROUTINE 

DESCRIPTION:

The EXIT ROUTINE statement enables you to exit from the executed routine.

The above example shows how EXIT ROUTINE can be used.

11.13.2 REPEAT ROUTINE

FORMAT:


        REPEAT ROUTINE 

EXAMPLE:


        10      get_username 
        20      END 
 
        12000   ROUTINE get_username 
                  INPUT PROMPT 'Username: ': uname$ 
                  IF  _BACK  or  _EXIT  THEN  EXIT ROUTINE 
                  IF  uname$ = ''  THEN  REPEAT ROUTINE 
                END ROUTINE 
 
        RNH 
        Username: 
        Username: Sunny 

DESCRIPTION:

The REPEAT ROUTINE statement enables you to repeat the entire routine execution. When INTOUCH executes the REPEAT ROUTINE statement, the routine is re-executed.

11.13.3 Private Variables in Routines

FORMAT:


        ROUTINE routine_name: PRIVATE var, var, var, ... 
    or 
        ROUTINE routine_name: PRIVATE INTEGER var, STRING var, STRING var, ... 

EXAMPLE:


        10   do_totals 
        20   END 
 
        100  ROUTINE do_totals: PRIVATE mytotal, desc$ 
               mytotal = 15 
               desc$ = 'Test Totals' 
               PRINT desc$; mytotal 
             END ROUTINE 
 
        rnh 
        Test Totals 15 

DESCRIPTION

INTOUCH allows you to use "private" variables in a routine. Private variables are variables identified with a specific routine. This option allows you to use the same variable names more than once because, internally, INTOUCH prefixes the variables with the routine name and a "$" character.

In the above example, the private variables "mytotal" and "desc$" are internally known to INTOUCH as:

do_totals$mytotal

do_totals$desc$

From inside the routine, you can reference the variables by their private names. For example: mytotal, desc$

From outside the routine (or while debugging the code), you can reference the private variables by their internal known names. For example: do_totals$mytotal, do_totals$desc$


Chapter 12
Exception Handling

Exception handling routines intercept run-time exceptions and execute a block of code which handles them. If you do not have an exception handler, INTOUCH returns an exception message specifying what the exception was and where it occurred. INTOUCH stops program execution or tries the offending statement again.

There are two types of exception handlers: attached handlers and detached handlers.

When you have an attached handler, you use a WHEN EXCEPTION IN statement. WHEN EXCEPTION IN puts the handler (the block of code to execute) right after the block of code it protects.

You can also have a detached handler. For a detached handler, use the statement WHEN EXCEPTION USE. When an exception occurs in the protected block, WHEN EXCEPTION USE calls a handler routine located in some other part of the program. The same handler routine can be used by any number of WHEN EXCEPTION USE statements and can be placed anywhere in your program.

This section explains exception handlers. It also describes the handling statements--RETRY, CONTINUE and EXIT HANDLER.

The following functions are also related to exception handling:

For a full description of these functions, see Section A.2, Other Functions.

12.1 CAUSE EXCEPTION

FORMAT:


        CAUSE EXCEPTION exception_number 

EXAMPLE:


        10  DO 
              INPUT 'Select a number between 1 and 10': no 
              IF  no < 1  OR  no > 10  THEN  CAUSE EXCEPTION 1001 
              REPEAT DO 
            END DO 
        20  END 
 
        RNH 
        Select a number between 1 and 10? 8 
        Select a number between 1 and 10? 99 
        Illegal number at 10.2 

PURPOSE:

Use CAUSE EXCEPTION when you need to generate an exception under specific conditions.

DESCRIPTION:

CAUSE EXCEPTION causes the specified exception to occur. exception_number can be any integer expression. When INTOUCH executes the CAUSE EXCEPTION statement, it generates the exception specified. See Section C.2, Exceptions for a list of exception messages.

12.2 WHEN EXCEPTION IN

FORMAT:


        WHEN EXCEPTION IN 
          --- 
          ---  protected block 
          --- 
        USE 
          --- 
          ---  handler 
          --- 
        END WHEN 

EXAMPLE:


        10  INPUT 'Your name, please': name$ 
            WHEN EXCEPTION IN 
              INPUT 'How old are you': age 
            USE 
              PRINT 'Not a valid age' 
              RETRY 
            END WHEN 
            PRINT 
            PRINT NAME$; ' is'; age 
        20  END 
 
        RNH 
        Your name, please? Tester 
        How old are you? 3x 
        Not a valid age 
        How old are you? 35 
 
        Tester is 35 

PURPOSE:

Use WHEN EXCEPTION IN to catch run-time exceptions and resolve them within your program when you want the code, handling the exception, to be next to the code you are protecting.

DESCRIPTION:

WHEN EXCEPTION IN begins the protected block of code. Everything between the WHEN EXCEPTION IN and USE statements constitutes the section of code protected by the handler---the protected block.

USE begins the handler routine. Everything between the USE and the END WHEN statements constitutes the handler. If an exception occurs in the protected block, the handler code is executed. END WHEN ends the routine.

12.3 WHEN EXCEPTION USE

FORMAT:


        WHEN EXCEPTION USE handl_name 
          ---               
          ---  protected block 
          --- 
        END WHEN 
                 . 
                 . 
                 . 
        HANDLER handl_name 
          --- 
          ---  handler 
          --- 
        END HANDLER 

EXAMPLE:


        10  INPUT 'Enter total sales amount': tsales 
            INPUT 'Enter number of sales': nsales 
            WHEN EXCEPTION USE fix_average 
              average = tsales/nsales 
            END WHEN 
            PRINT 'The average is:'; average 
 
        20  HANDLER fix_average 
              average = 0 
              CONTINUE 
        30  END HANDLER 
        40  END 
 
        RNH 
        Enter total sales amount? 25.00 
        Enter number of sales? 0 
        The average is: 0 

PURPOSE:

Use WHEN EXCEPTION USE to catch run-time exceptions and resolve them within a program when you need the same handler for several protected blocks, or when you want all the handlers in one place in your program.


Previous Next Contents Index