Thursday, June 18, 1998 Loops and Conditionals

There are 3 primary language constructs in all computer programming languages. They are actually very simple but can be used to perform complex operations. The first is the simplest, series or iteration. This means a series of commands, one after another, telling the computer to do one thing, then the next. The other two constructs rely on the computer's abiltiy to compare two items to one another. They are looping and comparison.

Comparison, usually referred to as conditionals, is when you compare two items then perform different functions based on the comparision. Conditionals are set to perform actions if the result of the comparison is true. A simple example in pseudo-code:

If (car is dirty) then wash car

The items compared are car and dirty. If the result of this comparison is true, then the wash car instruction is performed. The parentheses are not required, but help set the comparison apart from the rest of the statement, and later when you use multiple comparisons, using parenthesis will help prevent errors creeping it. This is a small example of pre-emptive debugging.

In ARexx, operators are used to make the comparisons, and to test for equality you use the equals sign =.

IF (car = dirty) THEN wash

Sometimes you will want to perform actions only if the result is false. You can invert the value of the result with the ~, also known as the NOT operator. This operator takes a variable, in this case the result of the comparison, and changes it to the opposite value. An example:

IF ~(car = clean) THEN wash

Now if the car is clean, the result of the comparison is TRUE, but we want to wash the car if it is NOT clean so the ~ inverts the result.

There are times when you will want to perform actions if the comparison is true, and other actions if the comparison is false. The ELSE keyword allows you to do this. Here is an example:

IF (car = clean) THEN
   drive
ELSE
   wash

In this example we spread the instruction out over several lines for readability. The ARexx interpreter generally takes lines into consideration, the end of the line terminates the current command, however with IF - THEN comparisons it knows to check the next line if the resulting instruction is not on the current line.

By nesting IF comparisons within one another, you can compare a variable to many different values, like this:

IF (x = 1) THEN
   SAY "x = 1"
ELSE IF (x = 2) THEN
   SAY "x = 2"
ELSE IF (x = 3) THEN
   SAY "x = 3"
ELSE
   SAY "x is unknown"

While this multiple comparison works, ARexx provices a simpler method using the SELECT keyword. This example demonstrates its usage:

SELECT
   WHEN (x = 1) THEN
      SAY "x = 1"
   WHEN (x = 2) THEN
      SAY "x = 2"
   WHEN (x = 3) THEN
      SAY "x = 3"
   OTHERWISE
      SAY "x is unknown"
END

The OTHERWISE keyword is used to perform actions if none of the other choices match. If the choices do not match and there is no OTHERWISE choice, the comparison terminates. Even if you know there shouldn't be a different value, another example of pre-emptive debugging is to have an OTHERWISE choice anyway, just in case.

If you want to perform more than one action in each condition, you need to enclose the list of actions within a DO - END group. This tells the interpreter that all the actions enclosed within the group are to be performed based on the comparison. Here is an example:

IF (car = dirty) THEN
   DO
      wash
      wax
   END

With the DO - END keywords, we can now move into the last programming structure, the loop. A loop is a repetition of a command or series of commands, with a control variable that terminates the loop. The simplest example would be:

DO 5
   SAY "Hello"
END

This example outputs the word "Hello" to the current CLI window 5 times. We could have written the SAY command 5 times, but the loop saves us time writing and performs the exact same function. The number 5 is the control variable telling the interpreter to perform the loop only 5 times.

Most loops make use of a conditional comparison for the control variable, and this can be very useful. For example:

x = 100
DO WHILE x > 10
   x = x / 2
END

There are two types of conditional loops. The first, like in the previous example, checks the comparison before the loop, and the second checks it after the loop, always executing the contents of the loop at least once. In ARexx the UNTIL keyword is used for the second type. In the WHILE loop the loop is continued as long as the comparison is TRUE. In the UNTIL loop, the loop is exited on a TRUE comparison.

x = 100
DO UNTIL x < 10
   x = x / 2
END

These loops have a variety of adjustments you can make to them which are far too complex to discuss at this time. When you are ready to look into this, check out any ARexx manual for information on DO. Always make sure that your comparison will validate properly, or you can easily get stuck in an infinate loop. One last example of looks is a simple loop where the control variable changes automatically.

DO x = 1 TO 5
   SAY "x = " x
END