Previous Next Chapter

Even.rexx

The IF instruction allows statements to be conditionally executed. In this example, the numbers from 1 to 10 are classified as odd or even by dividing them by 2 and then checking the remainder. The // arithmetic operator calculates the remainder after a division operation.

Program 4. Even.rexx

/*Even or odd?*/
DO i = 1 to 10 /*Begin loop - 10 iterations*/
IF 1 // 2 = 0 THEN type = `even'
ELSE type = `odd'
SAY i `is' type
END /*End loop*/

The IF line states that if the remainder of the division of the variable " i " by 2 equals 0, then set the variable " type " to even. If the remainder is not 0, the program will skip over the THEN branch and execute the ELSE branch, setting variable " type " to odd.

Top Previous Next Chapter