You are here: Home > Knowledge Refreshers

KR-212* REXX Day 1

 

(code will be in green and output will be in red – colors dedicated to the mainframes!)

  • We start straight with coding. We assume that you are doing everything on mainframes (though the concept of REXX is the same on other platforms as well).
  • Create a PDS; we’ll use USER.REXX.EXEC for our examples. LRECL of the PDS should be 80 (FB).
  • Create a member MYFIRST and go in edit mode and code what is given in the table.
  • First line as you might have guessed is a comment. Save the member and come to the screen where the pds members are listed. Type an EXEC before the member named MYFIRST and voila you have executed your first rexx code.
Code Output
/* THIS IS MY FIRST PROGRAM */
SAY "YIPEE THIS IS MY FIRST REXX CODE"
COMMAND SAY NOT FOUND OR REXX IDENTIFIER IS MISSING+ SUPPLY '/* REXX */' AS THE FIRST RECORD TO EXECUTE AS A REXX EXEC OR, FOR AN EXPLICIT EXEC, SUPPLY THE EXEC KEYWORD ON THE EXEC COMMAND

 

Did you try it? You’ll find a weirdo message that is given in the right side of the table. And with that we’ve started with a bang (or rather with an error!).

 

First lesson in rexx: Always start REXX codes with a comment that contains the word ‘REXX’ in the comment – it could be anywhere on the first line comment but it should be there somewhere.

 

Note: The word REXX is required on the first line mostly in mainframes only; on other platforms any comment will do – but you do need a comment to start your program. Modify the comment and execute the code to see your first successful rexx output!

 


 

KR-213* DAY 2 of REXX

  • Variable names: Feel free to use any name you like; anything and everything is allowed – except the usual restrictions like identifier cannot start with a number etc.
  • Variable type: Assign anything you want and REXX is smart enough to know what type of a variable you want to use.
  • Assign a number and it is numeric, assign a series of characters enclosed in single or double quotes and it is considered a string.
Code Output
/* REXX */
NUM = 1
SAY NUM
NUM = "1A"
SAY NUM
NUM = 'BB'
SAY NUM
1
1A
BB
/* REXX */
NUM = 1
NUM = NUM + 1
SAY NUM
NUM = "1A"
NUM = NUM + 1
2
6 +++ NUM = NUM + 1 Error running MYFIRST, line 6: Bad arithmetic conversion
  •  This code demonstrates that REXX is smart with respect to variables and also shows that REXX runs thro an interpreter and is not compiled.
  • The interpreter picks up each statement one at a time and executes it before proceeding to the next one.
  • There is no explicit variable declaration in REXX; we can use variables as and when required on the fly.


KR-214* DAY 3 of REXX (conditional statement and looping)
  • But before we get into that it is important to know how to represent a block of code in REXX. In C++ we have

    if (a == b)
    { -> the opening brace denotes start of the if body
    …..we have the body of the if statement inside the two braces
    } -> this closing brace signals the end of the if body

  • The code enclosed within the braces is called a block of code. If we didn’t specify the braces to mark the body of the if statement then only the first statement following the if will be considered as part of the if statement.
  • In REXX a block of code is demarcated within a DO and an END instead of the braces.
  • If else condition…

IF condition THEN
DO
/*body of if */
END
ELSE
DO
/*body of else*/
END

There is no END IF in rexx.

  • How many times do you think the following will loop? 4 or 5
DO I = 1 BY 1 UNTIL I = 5
/* body of loop */
END

Try it and see…




KR-215* DAY 4 of REXX (breaking from loops and SELECT statement)

Before we start let’s answer Friday’s question; how many times will the loop execute? Answer is 5 though many felt it is 4!

DO I = 1 BY 1 UNTIL I = 5
/* body of loop */
END

  • UNTIL -> Test condition after the loop executes at least once. Looping will happen as long as condition is false.
  • WHILE -> Tests condition before the loop executes even once; and looping will happen as long as the condition is true.
Breaking from loops:
  • LEAVE can be used to break out of a DO loop.
  • ITERATE is similar to ‘continue’ in C -> we continue the loop with the next value.
  • EXIT will terminate the program itself and quit.

Code Output
/* REXX */
DO I = 1 TO 4
DO J = 1 TO 3
  IF J = 3 THEN
     ITERATE I 
  SAY I J
END J
END I
EXIT
1 1
1 2
2 1 -> J = 3 never executes
2 2
3 1
3 2
4 1
4 2

In our example rexx iterates to the next value of “I” whenever J is 3.

SELECT statement:

Code Output
/* REXX */
SALES = 80

SELECT

WHEN SALES < 50 THEN
DO
  SAY "NOT UPTO THE MARK"
END
WHEN SALES < 100 THEN
DO
  SAY "DOING OKAY"
END
OTHERWISE
DO
  SAY "INVALID SALES FIGURE"
END

END
EXIT
DOING OKAY

If SALES were 30 output is:

NOT UPTO THE MARK

  • REXX will scan for a matching WHEN from the top to bottom; the minute a condition is satisfied it will execute that body and jump out of the select construct. It will not drop thro all conditions.