Jump to content

Rexx Programming/How to Rexx/switch conditional

From Wikibooks, open books for an open world

Rexx allows our program to examine numerous possible scenarios, one by one, until it finds a scenario which is actually the case. The keywords for doing this are SELECT, WHEN, THEN, and OTHERWISE. When we're done listing out scenarios and their possible responses, we close the construct with END.

/* Try to tell someone whether they have a fever or not. */
say "Enter your temperature in degrees Celsius:"
pull degrees
select
 when DataType(degrees) \= "NUM" then
  say "That's not a number. I can't help you."
 when degrees < 36.5 then
  say "Your body temperature is a bit low."
 when degrees > 37.5 then
  say "I think you have a fever."
 otherwise
  say "You're temperature seems normal."
end
/* NOT real medical advice -- just an example! */

After the example script receives its input, it starts running various tests. First, it checks whether the input is even a number. Then, it checks whether the temperature is too low, or too high. If neither of those conditions is met, it reports a normal temperature. After any of the WHEN conditions is met, the program stops checking, executes a line of code, and skips to the end.

Multiline responses

[edit | edit source]

You can allow more complex functionality by using DO/END blocks.

say "Would you like to convert feet to meters or meters to feet?"
say "Type M to enter a number of meters or F to enter a number of feet."
pull choice
select
 when choice = "M" then do
  say "Enter a number of meters:"
  pull meters
  feet = meters / 0.3048
  say "That's equal to" feet "ft."
 end
 when choice = "F" then do
  say "Enter a number of feet:"
  pull feet
  meters = feet * 0.3048
  say "That's equal to" meters "m."
 end
 otherwise say "I don't understand what you entered."
end

Testing more than one variable

[edit | edit source]

WHEN conditions can be as complex as we want them to be. Here's the famous FIZZBUZZ program, which counts normally except for multiples of 3, 5 and 15.

/* FizzBuzz */
do count = 1 to 30
 divisible_by_3 = count // 3 = 0
 divisible_by_5 = count // 5 = 0
 select
  when divisible_by_3 & divisible_by_5 then
   say "FIZZBUZZ"
  when divisible_by_3 then
   say "FIZZ"
  when divisible_by_5 then
   say "BUZZ"
  otherwise
   say count
 end
end

Isn't this just the same as IF/ELSE?

[edit | edit source]

Yes. If you've already read up on IF statements, you can write the same kind of code using those. SELECT is just an convenience for grouping related conditional constructs, especially if you'd need a whole lot of ELSE IF lines.

/* Code with SELECT: */
select
 when number < 0 then type = 'negative'
 when number > 0 then type = 'positive'
 otherwise type = 'neutral'
end
/* Same code with IF */
if number < 0 then type = 'positive'
else if number > 0 then type = 'positive'
else type = 'neutral'

Why "switch"?

[edit | edit source]

Control structures like this are sometimes called "switch conditionals" because some other languages use a "switch" keyword to analyze a single variable and branch to different execution pathways for different cases represented by different possible values. It often works with only a finite number of possible values and doesn't necessarily skip the other tests automatically. The Rexx version is more verbose but also more general. SELECT is often used in Rexx where other languages would have to use IF/ELSE.