Previous | Contents | Index |
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.
11.4 Conditionals
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.).
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 |
?25pc
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. |
Use the IF construct when you want to execute a statement or block of code only under specific conditions.
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.
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.
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.
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 |
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 |
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.
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 |
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 |
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 |
CHAIN 'file_spec' |
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 |
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:]
PASS [NOWAIT: | NORETURN:] str_expr |
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-1996 10:22:29.87 Total number of users = 1, number of processes = 1 Username Node Interactive Subprocess Batch TESTER TTI 1 |
Use PASS to perform system commands without leaving the INTOUCH environment or exiting a program.
INTOUCH supports using the PASS command from a captive account. This allows you to use INTOUCH 4GL for captive menu situations.
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.
10 PASS NOWAIT: 'show user t' 20 END INTOUCH RNH INTOUCH VAX/VMS User Processes at 9-MAY-1996 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.
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-1996 01:43:14.36 Total number of users = 1, number of processes = 1 Username Node Interactive Subprocess Batch TESTER TTI 1 $ |
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.
Previous | Next | Contents | Index |