Rexx 编程/Rexx 指南/控制结构
外观
控制结构(也称为控制构造)是在程序中用来基于给定条件控制程序流程的构造。控制结构通常使用条件分支指令或循环控制语句。控制结构中的代码通常以类似于 C 语言中控制结构的方式被组织成代码块。
以下示例展示了条件代码作为条件分支使用的情况。
if guess = 6 then say "Wow! That was a lucky guess."
这是一个包含两个部分的条件分支。
if guess = 6 then say "Wow! That was a lucky guess!" else say "Sorry, the number was actually 6."
如前所述,我们可以使用代码块,以便在条件满足(或不满足)时执行多个操作。
if score >= 100 then do say "Congratulations! You won." say "Go ahead and enter your initials below for our high score list." initials = LineIn() end else do say "Let's keep playing!" say "What do want your next move to be?" next_move = LineIn() end
Rexx 还提供了一种构造,用于从多个选项中选择第一个为真的条件。
select when age < 3 then say "You are a toddler." when age < 12 then say "You are a child." when age < 20 then say "You are a teenager." when age >= 65 then say "You are are a senior." when age >= 40 then say "You are middle-aged." otherwise say "You are a young adult." end
以下示例展示了代码块在循环中使用的情况。
do number = 1 to 10 say number end