Home > Software > Mainframe > Cobol
Interview Questions   Tutorials   Discussions   Programs   

Cobol - What is the difference between SEARCH and SEARCH ALL? What is more efficient?

asked mar August 3, 2014 01:17 AM  

What is the difference between SEARCH and SEARCH ALL? What is more efficient?


1 Answers

answered By Mswami   0  
SEARCH is a sequential search from the beginning of the table.
SEARCH ALL is a binary search, continually dividing the table in two halves until a match is found.
SEARCH ALL is more efficient for tables larger than 70 items.

Find below more details:

Sequential SEARCH / SEARCH:

During SERIAL SEARCH, the first entry of the table is searched. If the condition is met, the table look-up is completed. If the condition is not met, then index or subscript is incremented by one and the next entry is searched and the process continues until a match is found or the table has been completely searched.

    SET indexname-1 TO 1.
    SEARCH identifier-1 AT END display ‘match not found:’
       WHEN condition-1 imperative statement-1 /NEXT SENTENCE
       WHEN condition-2 imperative statement-2 /NEXT SENTENCE
Identifier-1 should be OCCURS item and not 01 item.
Condition-1, Condition-2 compares an input field or search argument with a table argument.
Though AT END Clause is optional, it is highly recommended to code that. Because if it is not coded and element looking for is not found, then the control simply comes to the next statement after SEARCH where an invalid table item can be referred and that may lead to incorrect results / abnormal ends.

SET statement Syntax:
    SET index-name-1 TO/UP BY/DOWN BY integer-1.


When the size of the table is large and it is arranged in some sequence –either ascending or descending on search field, then BINARY SEARCH would be the efficient method.

SEARCH ALL identifier-1 AT END imperative-statement-1
            WHEN dataname-1 = identifier-2/literal-1/arithmetic expression-1
               AND dataname-2 = identifier-3/literal-2/arithmetic expression-2

Identifier-2 and identifier-3 are subscripted items and dataname-1 and dataname-2 are working storage items that are not subscripted.
Compare the item to be searched with the item at the center. If it matches fine, else repeat the process with the left or right half depending on where the item lies.

   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!