Rexx - 决策



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

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

Decision Making

如果条件确定为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语句的流程图如下:

Nested If Statement

让我们来看一个嵌套if语句的例子:

示例

/* Main program */ 
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 子句都有一个不同的条件需要评估,然后执行后续语句。

  • 如果之前的 when 条件不评估为 true,则使用 otherwise 语句运行任何默认语句。

流程图

select语句的流程图如下:

Select Statement

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

示例

/* Main program */ 
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 
广告
© . All rights reserved.