Search

The Search verb in COBOL can be used to perform Linear searches, using table names as indexes.

Syntax:

SEARCH {TableName} [VARYING {IndexName}]             
                [AT END Code1]     
       {WHEN condition {Code2}                     
                       {NEXT SENTENCE    }
[END-SEARCH] 

Example:   

       WORKING-STORAGE SECTION.
           77 N PIC 99.
           77 SEARCHRNO PIC 99.
           01 ARRAY.
              02 ARR OCCURS 10 TIMES ASCENDING KEY IS RNO INDEXED BY I.
                03 RNO PIC 99.
        PROCEDURE DIVISION.  
            //AFTER ENTERING THE ELEMENTS IN ARRAY 
            //WE SEARCH THE ROLL NUMBER ENTERED 
            //BY USER IN VARIABLE SEARCHRNO.     
           SET I TO 1.
           SEARCH ARR AT END DISPLAY "NOT FOUND"
           WHEN RNO(I) = SEARCHRNO DISPLAY "FOUND ROLL NUMBER".
           STOP RUN.

  Output 1: To search the element that actually belongs to an array.

Output  2: To search the element that does not belong to an array.

Difference Between Search and Search All in COBOL

There are two ways in which we can perform the searching operations in COBOL, first using the traditional method i.e. by applying the loop with the help of PERFORM statement, or by using the predefined verbs: SEARCH and SEARCH ALL. SEARCH and SEARCH ALL verbs can be used only in the INDEXED FILES because they use the indexed variable associated with the file.

SEARCH VERB performs a linear search in the record/array whereas SEARCH ALL VERB performs the binary search in the record/array. 

Similar Reads

Search

The Search verb in COBOL can be used to perform Linear searches, using table names as indexes....

Search All

The Search All verb in COBOL is used to perform a Binary search in COBOL using the indexes(name of the table).ntax...