Open In App

Search All in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

Searching is one of the most important operations when it comes to performing operations on any data structure. We can perform searching operation in two ways :

  1. Linear Search: In this, we simply iterate through the data structure (array) and search for the given element in the array.
  2. Binary Search: In Binary search, we first find the middle element of the array,
    • If the searching element is equal to the middle element then the search operation ends here.
    • If the searching element is greater than the middle element then the data structure is divided into two parts and we search for the element in the second half and again check for the relational relation between the searching element and the middle element of the sub-array.
    • If the searching element is less than the middle element then the array is divided into two parts and we search for the element in the first half and again check for the relational relation between the searching element and the middle element of the sub-array.
    • It doesn’t matter which half it selects to search the element, only the middle element of both the half is compared to the searching element and it again’s check for the relational relation and divides the sub-array into two halves accordingly.This process continues either till the searching element is found or the array ends.

In COBOL to perform these searching operations we have two Verbs i.e. Search Verb for linear search and Search-All Verb for binary search. In this article we will learn about the Search-All Verb.

Search-All Verb performs fast operation and is more efficient as compared to Search Verb. In this case there is no need to use SET verb prior to SEARCH ALL verb cause it is done automatically by the Search-All Verb. There are few rules which must be considered before using the Search-All Verb:

  1. The array must be in sorted order form, i.e. it must be arranged either in ascending order or in descending order.
  2. This verb can be used only with the single-dimensional array.
  3. There is no need to assign the starting value to the Key before performing the operation.
  4. It only checks for the equality condition.
  5. If the element is not found in the array, then the imperative statement is executed and if there is no imperative statement then the control is passed to the next statement.

Syntax:

SEARCH ALL table name [ AT END imperative statement ]
WHEN condition-1  { imperative statement/NEXT SENTENCE } [END SEARCH].

Example:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. HELLOWORD.
ENVIRONMENT DIVISION.
DATA DIVISION.
      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"
        WHEN RNO(I) = SEARCHRNO DISPLAY "FOUND".
STOP RUN.


Output:

  • To search the roll number in the array that is actually available in the array.

  • To search the roll number in the array that is not available in the array.



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