COBOL 條件語句

2018-10-14 17:57 更新

條件語句(conditional statement)是用于根據(jù)程序員指定的某些條件更改執(zhí)行流。條件語句將始終計(jì)算為true或false。條件被使用在IF, Evaluate 和 Perform語句。不同類型的條件語句如下:

  • IF條件語句
  • 關(guān)系(relation)條件
  • 符號(hào)(sign)條件
  • 類(class)條件
  • 條件名稱(Condition-Name)條件
  • 否定(negated)條件
  • 組合(combined)條件

IF條件語句

IF語句檢查條件。如果條件為真(true),則執(zhí)行IF塊;如果條件為假(false),則執(zhí)行ELSE塊。
END-IF:用于結(jié)束IF塊。也可以使用一個(gè)句號(hào)替代END-IF結(jié)束IF塊。但是對(duì)于多個(gè)IF塊,最好使用END-IF。
Nested-IF(嵌套):IF塊出現(xiàn)在另一個(gè)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執(zhí)行上述COBOL程序:

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

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

關(guān)系條件

關(guān)系的條件比較兩個(gè)操作數(shù),或者可以是一個(gè)標(biāo)識(shí)符,文字,或算術(shù)表達(dá)式。不考慮大小和用法子句,完成數(shù)字字段的代數(shù)比較。

對(duì)于非數(shù)字操作數(shù)

如果比較兩個(gè)相同大小的非數(shù)字操作數(shù),則將字符從左至對(duì)應(yīng)的位置進(jìn)行比較,直到達(dá)到終點(diǎn)。包含更大數(shù)量字符的操作將被聲明為較大。

如果比較兩個(gè)大小不相等的非數(shù)字操作數(shù),則在較短的數(shù)據(jù)項(xiàng)的末尾添加空格,直兩個(gè)操作數(shù)的大小相等,然后根據(jù)前面提到的規(guī)則進(jìn)行比較。

語法

下面給出了關(guān)系條件語句的語法:

[Data Name/Arithmetic Operation]

          [IS] [NOT] 

[Equal to (=),Greater than (>), Less than (<), 
 Greater than or Equal (>=), 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執(zhí)行上述COBOL程序。

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-NUM1 IS GREATER THAN WS-NUM2

符號(hào)條件

符號(hào)條件用于檢查數(shù)字操作數(shù)的符號(hào)。它確定給定的數(shù)值是否大于,小于,或等于零。

語法

以下是符號(hào)條件語句的語法:

[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執(zhí)行上述COBOL程序:

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE

類條件

類條件用于檢查操作數(shù)是否只包含字母或數(shù)字的數(shù)據(jù)??崭裨贏LPHABETIC(字母),ALPHABTICTIC-LOWER(小寫字母)和ALPHABETIC-UPPER(大寫字母)中會(huì)被算入。

語法

以下是類條件語句的語法:

[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-NUM1 IS NUMERIC'.
   
STOP RUN.

JCL執(zhí)行上述COBOL程序。

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-NUM1 IS ALPHABETIC
WS-NUM1 IS NUMERIC

條件名條件

條件名稱是用戶定義的名稱。它包含一組由用戶指定的值,表現(xiàn)的類似于布爾變量。它們用級(jí)數(shù)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執(zhí)行上述COBOL程序:

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

Passed with 065 marks

否定條件

否定條件通過使用NOT關(guān)鍵字給出。如果一個(gè)條件為true,而我們沒有它的前面給出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執(zhí)行上述COBOL程序。

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

ELSE-BLOCK

組合條件

組合條件包含兩個(gè)或多個(gè)條件,使用邏輯運(yùn)算符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執(zhí)行上述COBOL程序。

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

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

Both condition OK

Evaluate(評(píng)估)動(dòng)詞

評(píng)估動(dòng)詞是一系列if-else語句的替代。它可用于評(píng)估多個(gè)條件。類似于在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執(zhí)行上述COBOL程序:

//示例作業(yè)(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
// STEP1 EXEC PGM = HELLO

當(dāng)你編譯和執(zhí)行上面的程序,會(huì)產(chǎn)生以下結(jié)果:

WS-A GREATER THAN 2
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)