Rexx 编程/如何使用 Rexx/goto
外观
许多语言都有一个“jump”指令或“goto”语句,用于跳至程序的任意部分。其他语言通过更特定的流程控制结构完全取代了对这种语句的需求。Rexx 主要使用子例程和 DO/END 块来跳转到程序的不同部分,但它确实有一种更通用的跳转指令。你可以使用 SIGNAL 关键字将计算机重定向到程序中带标签的位置。标签以冒号结尾。
/* 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
依赖 goto 语句的长程序众所周知从长期来看很难理解。坚持使用本书其他地方所述的结构化流程控制语句,比如 for 循环。编写此脚本的一种更简洁的方式(无需 SIGNAL 语句)如下
/* 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