Open In App

Difference Between Search and Search All in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

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.

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

Syntax:

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

Example:      

       WORKING-STORAGE SECTION.
           77 SEARCHRNO PIC 99.
           77 N 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.     
           SEARCH ALL ARR AT END DISPLAY "NOT FOUND ROLL NUMBER"
           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:

The below table has all major differences between Search and Search all:

SEARCH

SEARCH ALL

SEARCH verb is used to perform a linear search in COBOL. SEARCH ALL verb is used to perform binary search in COBOL.
For this the array is not required to be in the sorted form.  For this, the array must be in the sorted order form (either ascending or descending).
It performs searching operations sequentially and is also known as sequential search.  It performs searching operations by comparing the element with the middle element of the array, divides the array into two halves, and continues the process until the element is found.
It performs slow operations and is less efficient. It performs a fast operation and is more efficient
It can be used in single as well as multi-dimensional arrays. It can only be used in single-dimensional arrays.
Multiple conditions can be applied for searching using the WHEN. Only one equality condition can be applied.
The value of identifier-1 must be initialized prior to the SEARCH verb using the SET verb. There is no need to use the SET verb prior to SEARCH ALL verb and thus the incrementing is done automatically. 

Last Updated : 09 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads