How to Use IF Logic in COBOL SEARCH

In COBOL, we call an internal table as Array. This post explains how to use an IF condition in the Search statement.

You can give in WHEN statement conditions such as =, > and < in SEARCH.

About SEARCH and its real use, I have explained in my previous post differences of SEARCH and SEARCH ALL.


COBOL Search Logic
COBOL SEARCH IF Condition

TIP: SEARCH in COBOL is serial search. So sorting of internal table is not required

You can use IF condition to filter input fields along with SEARCH. The below sample code shows how to give IF condition:

SET IX TO 1.
SEARCH TABLE-ENTRY
AT END
   MOVE 1 TO ORG-SUB
   WHEN ORD-RECIPIENT = TABLE-CODE (IX)
        SET ORG-SUB        TO IX.
        MOVE ORD_CITY (IX) TO WS_CITY
        IF WS_CITY = '840'
           MOVE 'USA'      TO INP_REC (1:3)
        END-IF
END-SEARCH

Also read

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.