INTOUCH® 4GL
A Guide to the INTOUCH Language


Previous page... Table of Contents

11.5 IF/THEN ... END IF

FORMAT:

        IF cond_expr THEN statement [ELSE statement] 
 
                or 
 
        IF cond_expr1 THEN 
            --- 
            ---  block of code 
            --- 
        [ELSEIF cond_expr2 THEN 
            --- 
            ---  block of code 
            ---   ...] 
        [ELSE 
            --- 
            ---  block of code 
            ---      ] 
        END IF 

EXAMPLE:

        10  INPUT PROMPT 'Enter your age: ': age 
            INPUT PROMPT 'Enter your sex: ': sex$ 
        20  IF  UCASE$(sex$[1:1]) = 'M'  THEN  GOTO  done  ELSE PRINT 
        30  IF  age < 20  THEN 
              PRINT 'Please go to line A.' 
            ELSEIF  age > 19  AND  age < 40  THEN 
              PRINT 'Please go to line B.' 
            ELSE 
              PRINT 'Please go to line C.' 
            END IF 
        40  done: END 
 
        RNH 
        Enter your age: 25 
        Enter your sex: female 
 
        Please go to line B. 

PURPOSE:

Use the IF construct when you want to execute a statement or block of code only under specific conditions.

DESCRIPTION:

The simplest form of the IF construct is a one-line statement:

        IF cond_expr THEN statement 

cond_expr is a conditional expression. INTOUCH evaluates this expression as either TRUE (1) or FALSE (0). If the condition is TRUE, INTOUCH executes the statement following the THEN. If the condition is FALSE, INTOUCH skips the statement following the THEN and goes to the next line.

In the example program, when INTOUCH executes the first IF statement, it evaluates the conditional expression, SEX$[1:1] = 'M'. IF the user is 'Male' the condition is TRUE and INTOUCH executes the statement following the THEN and jumps to DONE:.

IF can be used to execute a block of code. The IF block construct looks like this:

        IF cond_expr THEN 
              ---       
              ---  block of code 
              --- 
        END IF 

If the conditional expression is TRUE, INTOUCH executes the block of code beginning on the next line. END IF marks the end of this block of code. If the expression is FALSE, INTOUCH skips to the statement following the END IF.

11.5.1 ELSE Option

The ELSE option executes a statement if the conditional expression is FALSE. The format of the IF statement with the ELSE option is:

        IF cond_expr THEN statement ELSE statement 

When INTOUCH executes the IF statement, it evaluates the conditional expression. If the expression is TRUE, the statement following the THEN is executed. If the expression is FALSE, the ELSE statement is executed. (Please refer to previous example.)

        RNH 
        Enter your age: 19 
        Enter your sex: Female 
 
        Please go to line A. 

In the above program, when INTOUCH executes the first IF statement, it evaluates the expression, SEX$[1:1] = 'M'. Since the user is Female, the expression is FALSE, so INTOUCH skips the THEN clause and jumps to the ELSE clause. INTOUCH executes the code between the ELSE clause and the END IF.

The ELSE option can be used to execute a block of code if the conditional expression is FALSE. The IF construct with the ELSE option looks like this:

        IF cond_expr THEN 
              --- 
              ---  block of code 
              --- 
        ELSE 
              --- 
              ---  block of code 
              --- 
        END IF 

If the conditional expression is TRUE, INTOUCH executes the block of code between the IF and the ELSE statements. If the expression is FALSE, INTOUCH executes the block of code between the ELSE and the END IF.

        10  INPUT PROMPT 'Enter your age: ': age 
            INPUT PROMPT 'Enter your sex: ': sex$ 
        20  IF  UCASE$(sex$[1:1]) = 'M'  THEN  GOTO the_end 
            PRINT 
        30  IF  age < 40  THEN 
              PRINT 'Please go to line A.' 
            ELSE 
              PRINT 'Please go to line B.' 
            END IF 
        40  the_end: END 
 
        RNH 
        Enter your age: 45 
        Enter your sex: female 
 
        Please go to line B. 

In the above program, when INTOUCH executes the second IF statement, it checks to see if "AGE < 40". Since AGE is not less than 40, the condition is FALSE. INTOUCH skips the code following the THEN and jumps to the ELSE clause. INTOUCH executes the code following the ELSE clause.

11.5.2 ELSEIF Option

The ELSEIF option sets up a new condition to check. The format of the IF construct with the ELSEIF option is:

        IF cond_expr1 THEN 
              --- 
              ---  block of code 
              --- 
        ELSEIF cond_expr2 THEN 
              --- 
              ---  block of code 
              ---            
        ELSE 
              ---                                                 
              ---  block of code 
              ---               
        END IF 

ELSEIF establishes a new condition. INTOUCH evaluates this condition. If the condition is TRUE (1), INTOUCH executes the code following the ELSEIF. If the condition is FALSE (0), INTOUCH jumps to the next clause in the IF construct.

        10  INPUT PROMPT 'Enter your age: ': age 
            INPUT PROMPT 'Enter your sex: ': sex$ 
        20  IF  UCASE$(sex$[1:1]) = 'M'  THEN  GOTO the_end ELSE PRINT 
        30  IF  age < 20  THEN 
              PRINT 'Please go to line A.' 
            ELSEIF  age > 19  AND  age < 40  THEN 
              PRINT 'Please go to line B.' 
            ELSE 
              PRINT 'Please go to line C.' 
            END IF 
        40  the_end: END 
 
        RNH 
        Enter your age: 25 
        Enter your sex: Female 
 
        Please go to line B. 

In the above program when INTOUCH executes the second IF statement, it checks to see if "AGE < 20". Since AGE is not less than 20, the first condition is FALSE. INTOUCH skips the code following the THEN and checks the condition set up by the ELSEIF. Since "AGE > 19" and "AGE < 40", the second condition is TRUE and INTOUCH executes the code following the ELSEIF and prints 'Please go to line B.', then exits the conditional.

11.6 SELECT CASE/END SELECT

FORMAT:

        SELECT CASE main_expr 
        CASE expr1[, expr2,...] 
            --- 
            ---  block of code 
            --- 
        [CASE expr3[, expr4,...] 
            --- 
            ---  block of code 
            ---            ...] 
        [CASE IS {numeric operator | boolean operator} expr5 
            --- 
            ---  block of code 
            ---            ...] 
        [CASE ELSE 
            --- 
            ---  block of code 
            ---            ...] 
        END SELECT 

EXAMPLE:

        10  DO 
              INPUT 'Your income per year': income 
              IF  _BACK  OR  _EXIT  THEN  EXIT DO 
        20    SELECT CASE income 
              CASE 0 
                PRINT 'No income?' 
              CASE IS < 0 
                PRINT 'A negative income?  You are in debt!' 
              CASE IS > 0 
                PRINT 'A positive income.' 
              END SELECT 
            LOOP 
        30  END 
 
        RNH 
        Your income per year? 0 
        No income? 
        Your income per year? -15000 
        A negative income? You are in debt! 
        Your income per year? 30000 
        A positive income. 
        Your income per year? exit 

PURPOSE:

Sometimes you need to compare one main expression with several values and execute a different block of code for each possible match. Use SELECT CASE to check a set of conditions and execute code depending on the results.

DESCRIPTION:

The SELECT CASE statement begins the construct and gives the main expression (main_expr). In the example, the main expression is INCOME. The CASE statements are compared with the main expression. The first CASE expression (expr1) is 0. Following this CASE is a block of code. If INCOME = 0 the block of code following CASE 0 is executed.

11.6.1 CASE expr, expr, expr...

Each CASE statement can include several expressions separated by commas. INTOUCH compares each of the expressions in a CASE statement. If any of them match, the block of code following the CASE is executed.

        10  DO 
              INPUT 'Procedure (add, del, exit)': pro$ 
              IF  _EXIT  THEN  EXIT DO 
              pro$ = UCASE$(pro$) 
        20    SELECT CASE pro$ 
              CASE 'ADD' 
                PRINT 'Adding...' 
              CASE 'DEL', 'DELETE' 
                PRINT 'Deleting...' 
              END SELECT 
            LOOP 
        30  END 
 
        RNH 
        Procedure (add, del, exit)? ADD 
        Adding... 
        Procedure (add, del, exit)? DEL 
        Deleting... 
        Procedure (add, del, exit)? EXIT 

The following example illustrates how to check for a range of values:

        10  a = 5 
            SELECT CASE a 
              CASE 1 : PRINT 'one' 
              CASE 2 to 6 : PRINT 'range' 
              CASE ELSE : PRINT 'else' 
            END SELECT 
        20  b$ = 'c' 
            SELECT CASE b$ 
              CASE 'a' : PRINT 'a' 
              CASE 'b' to 'e' : PRINT 'range' 
              CASE ELSE : PRINT 'else' 
            END SELECT 
        30  END 
 
        RNH 
        range 
        range 

11.6.2 CASE ELSE Option

The CASE ELSE option executes a block of code only if none of the CASE expressions match. SELECT CASE with the CASE ELSE option looks like this:

        SELECT CASE main expr 
        [CASE expr1, expr2... 
            --- 
            ---  block of code 
            ---            ...] 
        [CASE IS {numeric operator| boolean operator} expr3 
            --- 
            ---  block of code 
            ---            ...] 
        CASE ELSE 
            --- 
            ---  block of code 
            --- 
        END SELECT 

CASE ELSE must follow the last CASE statement. If none of the CASE expressions match, INTOUCH executes the block of code following the CASE ELSE statement.

        10  DO 
              INPUT 'Procedure (add, del, exit)' : pro$ 
              IF  _EXIT  THEN  EXIT DO 
              pro$ = UCASE$(pro$)  
        20    SELECT CASE pro$ 
              CASE 'ADD' 
                PRINT 'Adding...' 
              CASE 'DEL', 'DELETE' 
                PRINT 'Deleting...' 
              CASE ELSE 
                MESSAGE ERROR: 'Procedure must be: add, del or exit' 
                REPEAT DO 
              END SELECT 
            LOOP 
        30  END 
 
        RNH 
        Procedure (add, del, exit)? add 
        Adding... 
        Procedure (add, del, exit)? del 
        Deleting... 
        Procedure (add, del, exit)? funny   
 
                        Procedure must be add, del, or exit 
 
        Procedure (add, del, exit)? EXIT 

11.6.3 CASE IS Option

CASE IS lets you form a conditional expression to be checked against the main expression. The format of the CASE IS option is:

        CASE IS {relational operator} expr 

When the CASE IS statement executes, INTOUCH compares expr to the main_expr using the relational operator.

        10  DO 
              INPUT 'Your income per year': income 
              IF  _BACK  OR  _EXIT  THEN  EXIT DO 
        20    SELECT CASE income 
              CASE 0 
                PRINT 'No income?' 
              CASE IS < 0 
                PRINT 'A negative income?  You are in debt!' 
              CASE IS > 0 
                PRINT 'A positive income.' 
              END SELECT 
            LOOP 
        30  END 
 
        RNH 
        Your income per year? -15000 
        A negative income?  You are in debt! 
        Your income per year? 0 
        No income? 
        Your income per year? 25000 
        A positive income. 
        Your income per year? exit 

11.7 CHAIN Programs

FORMAT:

        CHAIN 'file_spec' 

EXAMPLE:

        10  LINE INPUT 'Your name (last, first)': name$ 
            OPEN #1: NAME 'storage.tmp', ACCESS OUTPUT 
            PRINT #1: name$ 
            CLOSE #1 
        20  INPUT 'Add to CLIENT structure (Y/N)': reply$ 
            IF  reply$ = 'Y'  THEN  CHAIN 'ADD' 
        30  END 
 
        RNH 
        Your name (last, first)? Woods, Jack 
        Add to CLIENT structure (Y/N)? N 

DESCRIPTION:

CHAIN exits the current program and runs the program specified.

file_spec is the specification for the program being chained to. The file specification can be any string expression. INTOUCH searches for the file specified. INTOUCH exits the current program and runs the program named. Control does not return to the current program when the chained program is finished. If INTOUCH cannot find the file, or if the file is not an executable program, an exception is generated.

When INTOUCH executes the CHAIN statement, it:

11.8 Pass Commands to the Operating System

11.8.1 PASS [NOWAIT: | NORETURN:]

FORMAT:

        PASS [NOWAIT: | NORETURN:] str_expr 

EXAMPLE:

        10  INPUT 'What would you like to show': info$ 
        20  PASS 'SHOW ' + info$ 
        30  END 
 
        RNH 
        What would you like to show? user t 
 
              VAX/VMS User Processes at  9-MAY-1995 10:22:29.87 
            Total number of users = 1,  number of processes = 1 
 
         Username  Node     Interactive  Subprocess   Batch 
         TESTER       TTI            1 

PURPOSE:

Use PASS to perform system commands without leaving the INTOUCH environment or exiting a program.

DESCRIPTION:

PASS passes the specified string expression to the operating system command interpreter. Generally, it passes the string to DCL. The operating system will respond to the string as it would if you entered it at the DCL $ prompt. When the system finishes, control returns to the INTOUCH program.

When you use the NOWAIT option with PASS, the operating system executes the passed command and immediately returns to INTOUCH without waiting for the passed command to finish.

EXAMPLE:

        10  PASS NOWAIT: 'show user t' 
        20  END 
 
        INTOUCH
 
        RNH 
 
        INTOUCH
 
              VAX/VMS User Processes at  9-MAY-1995 10:23:19.87 
            Total number of users = 1,  number of processes = 1 
 
         Username  Node     Interactive  Subprocess   Batch 
         TESTER       TTI            1 
 
        end 
 
        INTOUCH

The PASS NORETURN statement passes a command to the operating system but does not return to INTOUCH.

EXAMPLE:

        10  PRINT 'B E F O R E' 
        20  DELAY 2 
        30  PASS NORETURN: 'show user t' 
        40  END 
 
        RNH 
        B E F O R E 
 
              VAX/VMS User Processes at  9-MAY-1995 01:43:14.36 
            Total number of users = 1,  number of processes = 1 
 
         Username  Node     Interactive  Subprocess   Batch 
         TESTER      TTI            1 
        $ 

11.9 Branching

Branching means jumping from one part of a program to another. Branching is especially useful when combined with conditionals. For example, you can ask the user to choose a procedure. You can branch to different subroutines in your program, depending on the procedure chosen.

There are several statements used for branching: GOTO, GOSUB, DISPATCH, ON GOTO, and ON GOSUB. GOTO causes INTOUCH to jump to the specified location and continue normal program execution from there.

GOSUB and DISPATCH execute routines and subroutines. Subroutines are blocks of code that INTOUCH branches to, executes, and then returns from.

GOTO, GOSUB, and DISPATCH cause unconditional branching. When they are executed, INTOUCH jumps to the statement specified. To conditionally branch, combine these statements with conditional statements. Two other constructs, ON GOTO and ON GOSUB, are conditional. Both ON GOTO and ON GOSUB branch to one of a number of places in the program depending an integer expression. When the ON GOSUB construct is used, INTOUCH will return to the statement following the ON GOSUB when the subroutine has finished executing.

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


Next page... | Table of Contents