In this topic, we described about the Selective Programming Construction with detailed example.
Selective programming construction involves making the set of statements runs in conditional/selective manner. In selective programming construction, the statements execution decides by a condition.
Every condition has two selection flows (either condition true flow or condition false flow).Single line conditional statements are the part of selective programming construction.
COBOL has a below statements that are part of selective programming -
|IF||Evaluates a condition and decides the program flow depending on the evaluation.|
|EVALUATE||Provides a multi selection control during the program execution.|
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.