條件語句是用于改變根據(jù)由程序員指定的特定條件下的執(zhí)行流程。條件語句將總是為真或假。條件是使用在如果,計(jì)算和執(zhí)行的語句。不同類型的條件如下:
if語句檢查的條件。如果條件為True IF塊被執(zhí)行,如果條件為假ELSE塊被執(zhí)行。
END-IF被用來結(jié)束IF塊。到結(jié)束可以用來代替END-IF IF塊。但最好使用END IF的多個(gè)if塊。
嵌套IF:IF 模塊出現(xiàn)另一個(gè)IF塊內(nèi)。沒有限制嵌套IF語句。
以下是語法如果條件語句:
IF [condition] THEN [COBOL statements] ELSE [COBOL statements] END-IF.
實(shí)例
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程序執(zhí)行。
//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)系的條件是用來評(píng)估真假的兩個(gè)數(shù)值。數(shù)字字段的代數(shù)比較不論大小和用法子句完成。
對(duì)于非數(shù)字操作數(shù)
如果兩個(gè)相同大小的非數(shù)字操作數(shù)進(jìn)行比較,然后字符進(jìn)行比較,從左邊的對(duì)應(yīng)的位置,直到結(jié)束為止。含大字符操作數(shù)被聲明更大。
如果不相等大小的兩個(gè)非數(shù)字操作數(shù)進(jìn)行比較,然后在較短的數(shù)據(jù)項(xiàng)被追加了位已經(jīng)結(jié)束,直到操作數(shù)的大小成為相等,然后根據(jù)在先前的點(diǎn)所提到的規(guī)則相比較。
以下是關(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以上COBOL程序執(zhí)行。
//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
簽名條件用于檢查數(shù)字操作數(shù)的符號(hào)。操作數(shù)可以是:
以下是簽名條件語句的語法:
[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程序執(zhí)行。
//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ù)字?jǐn)?shù)據(jù)。空間被認(rèn)為是 ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER.
以下是類條件語句的語法:
[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以上COBOL程序執(zhí)行。
//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
一個(gè)條件名稱是用戶定義的名稱。它包含由用戶設(shè)置指定的值。它的行為就像布爾變量。他們定義了88級(jí)數(shù)。它不會(huì)有一個(gè)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程序執(zhí)行。
//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)鍵字給出。如果情況為true,我們已經(jīng)不考慮在它前面,,它的最終值將為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程序執(zhí)行。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當(dāng)編譯并執(zhí)行上面的程序它會(huì)產(chǎn)生以下結(jié)果:
ELSE-BLOCK
組合條件中包含兩個(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以上COBOL程序執(zhí)行。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當(dāng)編譯并執(zhí)行上面的程序它會(huì)產(chǎn)生以下結(jié)果:
Both condition OK
計(jì)算動(dòng)詞是更換一系列if-else語句。它可以被用于計(jì)算一個(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以上COBOL程序執(zhí)行。
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C //STEP1 EXEC PGM=HELLO
當(dāng)編譯并執(zhí)行上面的程序它會(huì)產(chǎn)生以下結(jié)果:
WS-A GREATER THAN 2