Best Example for COBOL SEARCH and WHEN condition

The below flow chart shows how Search works. So, SEARCH is a serial search. SEARCH ALL is a binary search.

Search

Key points:

  • Multiple WHEN conditions you can give
  • AND , ‘=’ , ‘>’, ‘<‘ operators you can give in WHEN condition
  • For ‘OR’ operator, the below way you can do coding in COBOL program
Search 
. .
WHEN A
WHEN B
imperative sentence . .
End-search
Read more: Best tutorial for COBOL serial search

Example for SEARCH:

01  TABLE-ONE.     
05 TABLE-ENTRY1 OCCURS 10 TIMES INDEXED BY TE1-INDEX.
10 TABLE-ENTRY2 OCCURS 10 TIMES INDEXED BY TE2-INDEX.
15 TABLE-ENTRY3 OCCURS 5 TIMES ASCENDING KEY IS KEY1 INDEXED BY TE3-INDEX.
20 KEY1 PIC X(5).
20 KEY2 PIC X(10). . . .
PROCEDURE DIVISION. . . .
SET TE1-INDEX TO 1
SET TE2-INDEX TO 4 S
ET TE3-INDEX TO 1
MOVE “A1234” TO KEY1 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)
MOVE “AAAAAAAA00” TO KEY2 (TE1-INDEX, TE2-INDEX, TE3-INDEX + 2)
. . .
SEARCH TABLE-ENTRY3
AT END
MOVE 4 TO RETURN-CODE
WHEN TABLE-ENTRY3(TE1-INDEX, TE2-INDEX, TE3-INDEX) = “A1234AAAAAAAA00”
MOVE 0 TO RETURN-CODE
END-SEARCH
Advertisements

Author: Srini

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