TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - SEARCH Statement


  • SEARCH is a serial search, which is used to find elements inside the table.

  • It can be used for both sorted and unsorted data inside the table.

  • The table needs to be defined with an index pharse.

  • When we use the search function, the function starts with the current setting of the index and it goes on until to the end of the table. The starting value of the index is left upto the choice of the programmer. We can set the value of the index value just before the search starts using the SET keyword.

  • If the searched element is not found, then the index is automatically incremented by 1 and it continues till the end of table.

SYNTAX:

SEARCH identifier [VARYING {identifier-2/index-name}]
 [AT END statement]
 [WHEN condition statements]
[END-SEARCH]

Example:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A PIC 9(1) OCCURS 10 TIMES INDEXED BY I.
01 WS-SRCH PIC 9(1) VALUE '0'.

PROCEDURE DIVISION.
   MOVE '1234567890' TO WS-TABLE.
   SET I TO 1.
   SEARCH WS-A
      AT END DISPLAY 'ZERO NOT FOUND IN TABLE'
      WHEN WS-A(I) = WS-SRCH
      DISPLAY 'NUMBER ZERO FOUND IN TABLE'
   END-SEARCH.
   STOP RUN.

When you compile and execute the above program, it produces the following result −

OUTOUT:

Ouput of the above program is "NUMBER ZERO FOUND IN TABLE". Because '0' is present in WS-TABLE table.

If '0' is not present WS-TABLE table, then the output of the above program is "ZERO NOT FOUND IN TABLE".

We will see format 2 in next chapter..

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal