Difference Between Search and Search All in COBOL - GeeksforGeeks

09 Sep, 2021
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. 
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.
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.
The below table has all major differences between Search and Search all:
SEARCH
SEARCH ALL
COBOL-Basics
Picked
COBOL
Difference Between
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Must Do Coding Questions for Product Based Companies
How to insert a pandas DataFrame to an existing PostgreSQL table?
Difference between var, let and const keywords in JavaScript
How to create an unique id in ReactJS ?
How to Convert Categorical Variable to Numeric in Pandas?
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Difference Between == and .equals() Method in Java