📜  COBOL-条件语句

📅  最后修改于: 2020-11-22 16:49:38             🧑  作者: Mango


条件语句用于根据程序员指定的某些条件来更改执行流程。条件语句将始终为true或false。在IF,Evaluate和Perform语句中使用条件。不同类型的条件如下-

  • IF条件声明
  • 关系条件
  • 签收条件
  • 课堂条件
  • 条件名称条件
  • 否定条件
  • 组合条件

IF条件声明

IF语句检查条件。如果条件为真,则执行IF块;否则,执行IF块。如果条件为假,则执行ELSE块。

END-IF用于结束IF块。要结束IF块,可以使用句点代替END-IF。但是,最好将END-IF用于多个IF块。

嵌套IF -IF块出现在另一个IF块中。嵌套IF语句的深度没有限制。

句法

以下是IF条件语句的语法-

IF [condition] THEN
   [COBOL statements]
ELSE
   [COBOL statements]
END-IF.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9).
   01 WS-NUM2 PIC 9(9).
   01 WS-NUM3 PIC 9(5).
   01 WS-NUM4 PIC 9(6).

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   MOVE 25 TO WS-NUM1 WS-NUM3.
   MOVE 15 TO WS-NUM2 WS-NUM4.
   
   IF WS-NUM1 > WS-NUM2 THEN
      DISPLAY 'IN LOOP 1 - IF BLOCK'
      
      IF WS-NUM3 = WS-NUM4 THEN
         DISPLAY 'IN LOOP 2 - IF BLOCK'
      ELSE
         DISPLAY 'IN LOOP 2 - ELSE BLOCK'
      END-IF
      
   ELSE
      DISPLAY 'IN LOOP 1 - ELSE BLOCK'
   END-IF.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

IN LOOP 1 - IF BLOCK
IN LOOP 2 - ELSE BLOCK

关系条件

关系条件比较两个操作数,两个操作数都可以是标识符,字面量或算术表达式。数值大小的代数比较不考虑大小和用法子句。

对于非数字操作数

如果比较两个大小相等的非数字操作数,则将字符从左侧与相应位置进行比较,直到到达末尾。包含更多字符的操作数被声明为更大。

如果比较两个不等大小的非数字操作数,则在较短的数据项的末尾添加空格,直到操作数的大小相等为止,然后根据前面提到的规则进行比较。

句法

下面给出的是Relation条件语句的语法-

[Data Name/Arithmetic Operation]

   [IS] [NOT] 

[Equal to (=),Greater than (>), Less than (=), Less than or equal (<=) ]

[Data Name/Arithmetic Operation] 

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(9).
   01 WS-NUM2 PIC 9(9).

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   MOVE 25 TO WS-NUM1.
   MOVE 15 TO WS-NUM2.
   
   IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN
      DISPLAY 'WS-NUM1 IS GREATER THAN WS-NUM2'
   ELSE
      DISPLAY 'WS-NUM1 IS LESS THAN WS-NUM2'
   END-IF.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

WS-NUM1 IS GREATER THAN WS-NUM2

签收条件

符号条件用于检查数字操作数的符号。它确定给定的数值是否大于,小于或等于零。

句法

以下是Sign条件语句的语法-

[Data Name/Arithmetic Operation] 

   [IS] [NOT] 

[Positive, Negative or Zero]

[Data Name/Arithmetic Operation]

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC S9(9) VALUE -1234.
   01 WS-NUM2 PIC S9(9) VALUE 123456.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   IF WS-NUM1 IS POSITIVE THEN
      DISPLAY 'WS-NUM1 IS POSITIVE'.
      
   IF WS-NUM1 IS NEGATIVE THEN
      DISPLAY 'WS-NUM1 IS NEGATIVE'.
      
   IF WS-NUM1 IS ZERO THEN
      DISPLAY 'WS-NUM1 IS ZERO'.
      
   IF WS-NUM2 IS POSITIVE THEN
      DISPLAY 'WS-NUM2 IS POSITIVE'.

STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE

课堂条件

类条件用于检查操作数是否仅包含字母或数字数据。空格按字母顺序,字母下方和字母上方考虑。

句法

以下是Class条件语句的语法-

[Data Name/Arithmetic Operation>]

   [IS] [NOT] 

[NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER]

[Data Name/Arithmetic Operation]

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC X(9) VALUE 'ABCD '.
   01 WS-NUM2 PIC 9(9) VALUE 123456789.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   
   IF WS-NUM1 IS ALPHABETIC THEN
      DISPLAY 'WS-NUM1 IS ALPHABETIC'.
      
   IF WS-NUM1 IS NUMERIC THEN
      DISPLAY 'WS-NUM1 IS NUMERIC'.
      
   IF WS-NUM2 IS NUMERIC THEN
      DISPLAY 'WS-NUM2 IS NUMERIC'.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

WS-NUM1 IS ALPHABETIC
WS-NUM2 IS NUMERIC

条件名称条件

条件名称是用户定义的名称。它包含一组由用户指定的值。它的行为类似于布尔变量。它们用级别编号88定义。它将没有PIC子句。

句法

以下是用户定义条件语句的语法-

88 [Condition-Name] VALUE [IS, ARE] [LITERAL] [THRU LITERAL].

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM PIC 9(3).
   88 PASS VALUES ARE 041 THRU 100.
   88 FAIL VALUES ARE 000 THRU 40.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   MOVE 65 TO WS-NUM.
   
   IF PASS 
      DISPLAY 'Passed with ' WS-NUM ' marks'.
      
   IF FAIL 
      DISPLAY 'FAILED with ' WS-NUM 'marks'.
      
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

Passed with 065 marks

否定条件

否定条件是通过使用NOT关键字给出的。如果条件为真并且我们在其前面未给出NOT,则其最终值将为false。

句法

以下是否定条件语句的语法-

IF NOT [CONDITION] 
   COBOL Statements
END-IF.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(2) VALUE 20.
   01 WS-NUM2 PIC 9(9) VALUE 25.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   
   IF NOT WS-NUM1 IS LESS THAN WS-NUM2 THEN
      DISPLAY 'IF-BLOCK'
   ELSE
      DISPLAY 'ELSE-BLOCK'
   END-IF.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

ELSE-BLOCK

组合条件

组合条件包含两个或多个使用逻辑运算符AND或OR连接的条件。

句法

以下是组合条件语句的语法-

IF [CONDITION] AND [CONDITION]
   COBOL Statements
END-IF.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 9(2) VALUE 20.
   01 WS-NUM2 PIC 9(2) VALUE 25.
   01 WS-NUM3 PIC 9(2) VALUE 20.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   
   IF WS-NUM1 IS LESS THAN WS-NUM2 AND WS-NUM1=WS-NUM3 THEN
      DISPLAY 'Both condition OK'
   ELSE
      DISPLAY 'Error'
   END-IF.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

Both condition OK

评估动词

评估动词是一系列IF-ELSE语句的替代。它可以用于评估多个条件。它类似于C程序中的SWITCH语句。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-A PIC 9 VALUE 0.
   
PROCEDURE DIVISION.
   MOVE 3 TO WS-A.
   
   EVALUATE TRUE
      WHEN WS-A > 2
         DISPLAY 'WS-A GREATER THAN 2'

      WHEN WS-A < 0
         DISPLAY 'WS-A LESS THAN 0'

      WHEN OTHER
         DISPLAY 'INVALID VALUE OF WS-A'
   END-EVALUATE.
   
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

当您编译并执行上述程序时,它将产生以下结果-

WS-A GREATER THAN 2