if booleanExpression then ... else ... |
The if instruction group is an important programming concept. The execution of one or more instructions is performed when a condition is true. An alternative series of instructions is optionally performed when the condition is false.
In Rexx, conditions are evaluated as boolean expressions. A boolean expression is true if the value is '1', and false if the value is '0'. Any other values are invalid.
The condition is concluded when the end of the if source line is encountered. The source line may have been continued across multiple lines. If the then keyword is encountered on the same source line, or continued source lines, the condition is immediately concluded. Thus, the then keyword is special and cannot be used within the context of the conditional expression.
Here are various forms of if instruction usage. These are presented using a style which has improved readability. Notice that the then keyword is placed on the same line as the if instruction. When present the optional do keyword is on the same line as the then or else keywords. The instructions associated with the then or else are indented. The corresponding end keyword is at the same level of indentation as the prior instructions.
if booleanExpression then
instruction if booleanExpression then instruction else instruction if booleanExpression then do instruction instruction ...etc end if booleanExpression then instruction else do instruction instruction ...etc end if booleanExpression then do instruction instruction ...etc end else do instruction instruction ...etc end if booleanExpression then do instruction instruction ...etc end else instruction |
Within the series of instructions, another if instruction group can be specified. This can introduce a difficulty. An else sequence binds to the nearest incomplete if instruction. Consider the following example:
say 'What is your favorite color?'
color = translate( linein() ) if color = 'RED' then
|
When the selected color is 'RED', and the time is between noon and midnight, the above example shows:
that's a hot color
When the selected color is 'RED', and the time is between midnight and noon, the above example shows:
ok
When the selected color is not 'RED', nothing is displayed -- SURPRISE! This is because the else binds with the if instruction that tested the time, instead of binding with the if instruction that tested the color. You might be confused because the structure of the logic appears to connect the else with the first if. However, as mentioned above, the else sequence binds to the nearest incomplete if instruction.
The following shows how the logic is actually structured:
say 'What is your favorite color?'
color = translate( linein() ) if color = 'RED' then
|
Other programming languages use isolated semicolons as empty instructions. In Rexx programs, isolated semicolons are not empty instructions. The NOP instruction is explicitly used for this purpose instead. For example, the following program is erroneous:
say 'Please enter your favorite color:'
parse pull color if color = 'RED' then ; /* <-- ERROR !!! */ else say 'your favorite color is not red' |
The correct way to program this in Rexx uses the NOP instruction instead.
say 'Please enter your favorite color:'
parse pull color if color = 'RED' then nop /* <-- ah, this is correct */ else say 'your favorite color is not red' |
A common programming idiom is a cascading series of if ... else if ... else ... instruction groups. These can have do ... end instruction sequences also. The following shows the structure of this idiom.
if condition1 then
instruction else if condition2 then instruction else if condition3 then instruction ...etc. else
|
An alternative approach to the above cascade uses the select instruction instead. These can have do ... end instruction sequences also. The following shows the structure of this idiom when a select instruction group is used.
select
when condition1 then instruction when condition2 then instruction when condition3 then instruction ...etc. otherwise
end |