Summary -

In this topic, we described about the below sections -

The SEARCH statement searches a table for an element specified in the condition. The SEARCH adjusts the associated index once the element found in the table. This SEARCH is called as serial search or linear search or simple search.

Syntax - Serial Search

SEARCH Syntax

identifier-1

Identifier-1 specifies the table name to be searched. identifier-1 references all occurrences within the table. The declaration for identifier-1 must contain an OCCURS clause. The table can be searched using an index defined for a different table.

Identifier-1 can reference a data item that is subordinate to a data item with an OCCURS clause. In this case, it must specify an INDEXED BY phrase for each dimension of the table. Identifier-1 must not be subscripted or reference-modified.

VARYING phrase

Specifies the index from another table is used for search.

index-name-1

If index-name-1 is an index for table identifier-1, index-name-1 is used for the search. Otherwise, the index defined with INDEXED BY phrase is used for the search.

When the VARYING index-name-1 phrase is omitted, the index defined with INDEXED BY phrase is used for the search. If another table index is used to search a table without an INDEXED BY phrase, correct results are ensured only if both the table have table elements of the same length and with the same number of occurrences.

identifier-2

Must be either an index data item or an elementary integer item. Identifier-2 can't be a date field. Identifier-2 can't be subscripted by the index-name specified for identifier-1. During the search, one of the following actions applies -

  • If identifier-2 is an index, then the search index is increased and the specified index data item is simultaneously increased by the same size.
  • If identifier-2 is subscript, then the search index is increased and the specified data item is simultaneously increased by 1.

AT END

The condition occurred when the search operation terminates without satisfying the condition specified in any of the WHEN phrases.

AT END and WHEN phrases

After imperative-statement-1 or imperative-statement-2 is executed, control passes to the end of the SEARCH statement.

NEXT SENTENCE

NEXT SENTENCE transfers control to the first statement following the closest separator period.

END-SEARCH phrase

The END-SEARCH used to end the scope of the SEARCH statement. END-SEARCH is not required when SEARCH statement ended with period at the end. END-SEARCH permits conditional SEARCH to be nested in another conditional statement.

Searching Process and Requirements -

Before executing a serial search for the first time, index value must set to indicate the starting occurrence for the search. For serial search on a multidimensional table, index value must set for each superordinate dimension.

The SEARCH statement modifies only the value in the search index and if the VARYING phrase is specified, the value in index-name-1 or identifier-2. To search an entire two-dimensional to seven-dimensional table, SEARCH statement must execute for each dimension.

Case-1: - When the search begins, if the index value with identifier-1 is not greater than the max occurrence number, the search works like below -

Step-1: -The conditions in the WHEN phrase is evaluated in the order they are written.

Step-2: -If none of the conditions is satisfied, the index for identifier-1 is increased to the next table element and repeat step-1.

Step-3: -If one of the WHEN conditions is satisfied, the search is terminated immediately and the imperative-statement-2 associated with that condition is executed. The index points to the table element that satisfied the condition. If NEXT SENTENCE is specified, control passes to the statement following the closest period.

Step-4: -If the end of the table is reached (that is, index value is greater than the max occurrence number) without the WHEN condition being satisfied, the search is terminated.

Case-2: - When the search begins, the index value with identifier-1 is greater than the max occurrence number, the search terminates immediately.

When the search terminates and if the AT END phrase is specified, imperative-statement-1 is executed. If the AT END phrase is omitted, control passes to the next statement after the SEARCH statement.

Single dimensional array Practical Example - SEARCH

Below example used to display the student passed or failed status by searching single dimensional array.

Code:


IDENTIFICATION DIVISION.                                        
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
 01 SECTION. 
   02 STUDENT. 
     03 SUBJECT PIC 9(3) OCCURS 6 TIMES INDEXED BY SEQ.
PROCEDURE DIVISION.                                             
    PERFORM SEQ FROM 1 BY 1 UNTIL SEQ > 6 
	ACCEPT MARKS[SEQ] 
    END-PERFORM. 
	   
    SET SEQ TO 1.
	   
    SEARCH STUDENT VARYING SEQ
	AT END DISPLAY 'STUDENT PASSED' 
	WHEN SUBJECT[SEQ] < 35 
	     DISPLAY 'STUDENT FAILED' 
    END-SEARCH. 
							       
    STOP RUN.                             

Two dimensional array Practical Example - SEARCH

Below example used to display passed or failed status for two students by searching two dimensional array.

Code:


IDENTIFICATION DIVISION.                                        
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
 01 SECTION 
  02 STUDENT OCCURS 2 TIMES INDEXED BY S. 
    03 SUBJECT PIC 9(3) OCCURS 6 TIMES.INDEXED BY M.
PROCEDURE DIVISION.                                             
                                 
   PERFORM S FROM 1 BY1 UNTIL S > 2 
   PERFORM M FROM 1 BY 1 UNTIL M > 6 
      ACCEPT MARKS[S,I] 
   END-PERFORM 
   END-PERFORM.

   PERFORM S FROM 1 BY1 UNTIL S > 2 
   SET I TO 1. 
   SEARCH STUDENT 
       AT END DISPLAY 'STUDENT PASSED' 
   WHEN SUBJECT[S,I] < 35 
        DISPLAY 'STUDENT FAILED' 
   END-SEARCH 
   END-PERFORM. 
	   
   STOP RUN.