Open In App

Search in COBOL

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The SEARCH keyword is used to check for the presence or absence of some particular elements in any tables or arrays. We can find an element either by performing loop operations or by using the SEARCH keyword. We can even go for SEARCH ALL but this article will read about the SEARCH keywords only.

Syntax:

SEARCH table-name [ VARYING index ]

[AT END imperative statement-1 ]

 { WHEN condition-1 {statement-2, NEXT SENTENCE } }                        

[ END- SEARCH ].

Key Points :

  • SEARCH keyword is used as the Linear Search in the COBOL programming language.
  • Table/Array entries need not be in any proper sequence.
  • It will perform the operation in the sequential order, hence also known as sequential search.
  • It can include multiple conditions using the WHEN clause
  • It requires a SET statement before the SEARCH statement.
  • Being a slow operation, it is considered less efficient.

Example:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. SEARCHS-EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LETTER-TABLE VALUE 'QWERTYUIOPASDFGHJKLZXCVBNM' .
    05 LETTER-ENTRY OCCURS 26 TIMES INDEXED BY IDX.
        10 LETTER-VAR PIC X(1).
01 LETTER-COMP PIC X(1) VALUE '*'.
PROCEDURE DIVISION.
MAIN-PARA.
    SET IDX TO 1.
    SEARCH LETTER-ENTRY
        AT END DISPLAY LETTER-COMP ' NOT FOUND LETTER ' 
        WHEN LETTER-COMP = LETTER-VAR(IDX)
            DISPLAY 'LETTER Z IS FOUND IN THE TABLE '
    END-SEARCH
STOP RUN.


Output:

 

 

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads