Sample EXEC 2 Language Program

The following exec, called ADD, asks you for three numbers that you want to add, determines if you entered numeric data, and adds the three numbers you entered.

ADD EXEC contains the following:
&TRACE
&ERROR &EXIT &RETCODE
&TYPE Enter three numbers you want to add:
&READ ARGS
&IF &N = 3 &GOTO -ADDNUMS
&TYPE You must enter three numbers
&READ ARGS
&IF &N = 3 &GOTO -ADDNUMS
&GOTO -ERROR
-ADDNUMS
&TYPE1  = &DATATYPE OF &1
&TYPE2  = &DATATYPE OF &2
&TYPE3  = &DATATYPE OF &3
&IF &TYPE1 NE NUM &GOTO -ERROR
&IF &TYPE2 NE NUM &GOTO -ERROR
&IF &TYPE3 NE NUM &GOTO -ERROR
&SUM = &1 + &2 + &3
&TYPE The sum of  &1  &2  and  &3  is  &SUM
&EXIT
-ERROR
&TYPE You did not enter three valid items.  This program is ending.
&EXIT
The following describes the sequence of execution:
  • The &TRACE indicates to the system that this exec is written in the EXEC 2 language.
  • The &ERROR control statement specifies that if a z/VM command results in a nonzero return code, the &EXIT statement is executed. In this example, the return code, indicated by the control word &RETCODE, is passed upon exit.
  • The &TYPE statement asks you to enter three numbers. The &READ ARGS statement reads the items you enter and assigns them to the variables &1, &2, and &3.
  • The &IF statement checks to see if you entered three items. If you did not, you are asked again to enter three numbers. If you still do not enter three items, an error message is displayed and the program ends.
  • If you entered three items, execution flows to -ADDNUMS. The next six lines check to see if you entered only numbers and not any other characters. The &DATATYPE OF function yields a value of NUM if &1, &2, or &3 is a valid number and yields a value of CHAR if &1, &2, or &3 is anything else. If &TYPE1, &TYPE2, or &TYPE3 does not contain NUM, execution flows to -ERROR and the program ends.
  • If &TYPE1, &TYPE2, or &TYPE3 contains NUM, then &1, &2, and &3 are added together and the sum is displayed.

Many EXEC 2 facilities are similar to CMS EXEC facilities. Some control statements and special variables have not been covered here. For full details on the EXEC 2 processor facilities, see the VM/SP: EXEC 2 Reference.