COBOL Selective Programming ConstructionCOBOL Move Reference Modification COBOL IF Statement
The statements in the program are running in a sequence until or unless if any statement executed in the flow that will alter the execution sequence.
In the program, there might be some need to execute some set of statements in a selective manner. i.e. based on some conditions.
Selective programming construction involves making the program execution in selective manner.
Single line conditional statements are the part of selective programming construction.
COBOL has a special set of commands for the selective programming as well like below.
IF statement decide the program flow based on the condition validation.
IF statement comes under condition validation programming.
IF statement mainly three types based on its usage in the program.
- Simple IF
- IF ELSE
- Nested IF
Simple IF mainly used to execute the condition specific code.
In IF, the set of statements executed if the specified condition is satisfied.
IF condition not satisfied, the control will transfer to the next statements after IF statement terminated.
The simple IF statement explained in detail here
IF ELSE statement used when certain set of statements flow needs to be decided by two conditions.
IF ELSE mainly used to execute the condition specific code.
In IF ELSE, the set of statements executed if the specified condition is satisfied.
IF condition not satisfied, the other set of statements will be executed and those will set will be under ELSE part.
The IF ELSE statement explained in detail here
COBOL allows the NESTED IF statement. IF statement with in the IF statement called as NESTED IF statement.
NESTED IF contains a chain of conditions which are more than two.
NESTED IF mainly used to execute the condition specific code.
The NESTED IF statement explained in detail here
EVALUATE provides a multi selection control during the program execution.
EVALUATE used to validate multiple conditions in a single shot.
EVALUATE statement in COBOL is similar to Case or Switch statements of other languages.
EVALUATE can do multiple IF conditions task.
The EVALUATE statement explained in detail here
Example with a combination of IF and EVALUATE statements.
IDENTIFICATION DIVISION. PROGRAM-ID. IFCOND. DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-DETAILS. 02 STD-NUM PIC 9(03). 02 STD-NAME. 03 STD-NAME-INIT PIC X(01). 88 STD-NAME-VALID VALUE 'A' THRU 'Z'. 03 STD-NAME-REST PIC X(14). 02 STD-GENDER PIC X(01). 88 VALID-GENDER VALUE 'M' 'F'. 02 STD-MARKS PIC 9(03). 88 FIRST-CLASS VALUE 060 THRU 100. 88 SECOND-CLASS VALUE 050 THRU 059. 88 THIRD-CLASS VALUE 035 THRU 049. 88 FAIL VALUE 000 THRU 034. PROCEDURE DIVISION. ACCEPT STD-NUM. ACCEPT STD-NAME. ACCEPT STD-MARKS. DISPLAY 'STUDENT DETAILS....'. DISPLAY 'STUDENT NUMBER : ' STD-NUM. * SIMPLE IF CONDITION IF STD-NAME-VALID DISPLAY 'STUDENT NAME : ' STD-NAME END-IF. * IF ELSE CONDITION IF VALID-GENDER DISPLAY 'STUDENT GENDER : ' STD-GENDER ELSE DISPLAY 'STUDENT GENDER GIVEN WAS INVALID' END-IF. * EVALUATE EVALUATE STD-MARKS WHEN 60 THRU 100 DISPLAY 'STUDENT GOT FIRST CLASS ' WHEN 50 THRU 59 DISPLAY 'STUDENT GOT SECOND CLASS ' WHEN 35 THRU 49 DISPLAY 'STUDENT GOT THIRD CLASS ' WHEN OTHER DISPLAY 'STUDENT FAILED ' END-EVALUATE. STOP RUN.
COBOL Move Reference Modification COBOL IF Statement