Rexx Programming/How to Rexx/goto
Many languages have a "jump" instruction or a "goto statement" that skips to an arbitrary part of the program. Other languages completely replace the need for such a statement with more specific flow control constructs. Rexx mostly uses subroutines and DO/END blocks to jump around to different parts of the program, but it does have a more general kind of jump instruction. You can use the SIGNAL keyword to redirect the computer to a labeled location in the program. Labels end in a colon.
/* Jump around to validate input and factor a whole number. */ START: say "I can factor a positive whole number for you." say "First, enter a number of your choice:" signal GET_NUMBER START_FACTORING: say "Here are the factors of" number":" possible_factor = 1 CHECK_FACTOR: if number // possible_factor = 0 then signal SHOW_FACTOR else signal NEXT_FACTOR SHOW_FACTOR: say possible_factor NEXT_FACTOR: possible_factor = possible_factor + 1 if possible_factor > number then signal FINISHED signal CHECK_FACTOR FINISHED: say "That's all!" exit GET_NUMBER: pull number CHECK_NUMBER: if DataType(number, "W") then signal POSITIVE? say "That's not even a whole number." signal GET_NUMBER POSITIVE?: if number > 1 then signal START_FACTORING say "Positive, please!" signal GET_NUMBER
Long programs that depend on goto statements are notoriously difficult to understand in the long term. Stick with the structured control flow statements described elsewhere in this book, such as for loops. A more elegant way to write this script, without SIGNAL statements, might be as follows:
/* Greet the user and ask for a positive whole number. */ say "I can factor a positive whole number for you!" say "First, enter a number of your choice:" /* Make sure the user actually enters a positive whole number. */ do until whole? & positive? pull number whole? = DataType(number, "W") positive? = number > 0 if \ whole? then say "That's not even a number!" else if \ positive? then say "Positive, please!" end /* Now show the factors */ say "Here are the factors of" number":" do possible_factor = 1 to number if number // possible_factor = 0 then say possible_factor end say "That's all!" exit