Rexx - 决策语句

决策结构要求程序员指定一个或多个由程序评估或测试的条件。

下图显示了大多数编程语言中典型决策结构的一般形式。

决策

如果确定条件为true,则有一个或多个语句要执行,并且可选地,如果确定条件为 false,则要执行其他语句。

让我们看看 Rexx 中提供的各种决策语句。

序号 语句 & 描述
1 if 语句

第一个决策语句是if语句。 if 语句由一个布尔表达式后跟一个或多个语句组成。

2 if-else 语句

下一个决策语句是 if-else 语句。 if 语句后面可以跟一个可选的 else 语句,该语句在布尔表达式为 false 时执行。

嵌套 If 语句

有时需要将多个 if 语句相互嵌入,这在其他编程语言中是可能的。 在 Rexx 中这也是可能的。

语法

if (condition1) then 
   do 
      #statement1 
   end 
else 
   if (condition2) then 
      do 
      #statement2 
   end

流程图

嵌套if语句的流程图如下 −

嵌套 If 语句

让我们以嵌套 if 语句为例 −

示例

/* 主程序 */
i = 50 
if (i < 10) then 
   do 
      say "i is less than 10" 
   end 
else 
if (i < 7) then 
   do 
      say "i is less than 7" 
   end 
else 
   do 
      say "i is greater than 10" 
   end 

上述程序的输出将是 −

i is greater than 10 

select 语句

Rexx 提供了 select 语句,可用于根据 select 语句的输出执行表达式。

语法

该语句的一般形式是 −

select 
when (condition#1) then 
statement#1 

when (condition#2) then 
statement#2 
otherwise 

defaultstatement 
end 

该语句的一般工作原理如下 −

  • select 语句有一系列when 语句来评估不同的条件。

  • 每个when子句都有一个不同的条件,需要对其进行评估并执行后续语句。

  • otherwise 语句用于在前面的条件评估为 true 时运行任何默认语句。

流程图

select语句的流程图如下

select 语句

以下程序是 Rexx 中 case 语句的示例。

示例

/* 主程序 */
i = 50 
select 
when(i <= 5) then 
say "i is less than 5" 

when(i <= 10) then 
say "i is less than 10" 

otherwise 
say "i is greater than 10" 
end

上述程序的输出为 −

i is greater than 10