目录

Rexx - 决策( Decision Making)

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

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

做决定

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

让我们看看Rexx中提供的各种决策声明。

Sr.No. 声明和说明
1 If statement

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

2 If-else statement

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

嵌套If语句

有时需要将multiple if statements嵌入到彼此内部,这在其他编程语言中是可能的。 在Rexx中,这也是可能的。

语法 (Syntax)

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

流程图 (Flow Diagram)

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

嵌套If语句

让我们举一个嵌套if语句的例子 -

例子 (Example)

/* 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 

选择语句

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

语法 (Syntax)

本声明的一般形式是 -

select 
when (condition#1) then 
statement#1 
when (condition#2) then 
statement#2 
otherwise 
defaultstatement 
end 

本声明的一般工作如下 -

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

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

  • 如果前面的条件不evaluate to true ,则otherwise语句用于运行任何默认语句。

流程图 (Flow Diagram)

select语句的流程图如下

选择声明

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

例子 (Example)

/* 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 
<上一篇.Rexx - 循环
Rexx - Numbers.下一篇>
↑回到顶部↑
WIKI教程 @2018