MainframeMaster

COBOL Tutorial

COBOL SEARCH Statement - Quick Reference

Progress0 of 0 lessons

Overview

The SEARCH statement is used to search through tables and arrays in COBOL. It provides both sequential and binary search capabilities for finding specific values within data structures.

Purpose and Usage

  • Table searching - Search through tables and arrays
  • Data lookup - Find specific values in data structures
  • Validation - Validate data against reference tables
  • Code lookup - Look up codes and descriptions
  • Efficient searching - Use binary search for large tables

Syntax

The SEARCH statement supports both sequential and binary search methods.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
* Basic SEARCH syntax SEARCH table-name WHEN condition statements WHEN condition statements AT END statements END-SEARCH * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. SEARCH-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 LOOKUP-TABLE. 05 TABLE-ENTRY OCCURS 10 TIMES. 10 CODE PIC X(3). 10 DESCRIPTION PIC X(20). 01 SEARCH-CODE PIC X(3). 01 FOUND-FLAG PIC X(1) VALUE 'N'. PROCEDURE DIVISION. MAIN-LOGIC. MOVE 'ABC' TO SEARCH-CODE PERFORM SEARCH-TABLE STOP RUN. SEARCH-TABLE. SEARCH TABLE-ENTRY WHEN CODE(SEARCH-INDEX) = SEARCH-CODE MOVE 'Y' TO FOUND-FLAG DISPLAY "Found: " DESCRIPTION(SEARCH-INDEX) AT END DISPLAY "Code not found" END-SEARCH.

SEARCH provides flexible table searching capabilities.

Practical Examples

Examples of using the SEARCH statement in different scenarios.

Code Lookup Table

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* Code lookup with SEARCH DATA DIVISION. WORKING-STORAGE SECTION. 01 STATE-CODES. 05 STATE-ENTRY OCCURS 50 TIMES. 10 STATE-CODE PIC X(2). 10 STATE-NAME PIC X(20). 01 INPUT-CODE PIC X(2). 01 FOUND-STATE PIC X(20). PROCEDURE DIVISION. LOOKUP-STATE. MOVE INPUT-CODE TO SEARCH-CODE SEARCH STATE-ENTRY WHEN STATE-CODE(SEARCH-INDEX) = INPUT-CODE MOVE STATE-NAME(SEARCH-INDEX) TO FOUND-STATE DISPLAY "State: " FOUND-STATE AT END DISPLAY "Invalid state code" END-SEARCH.

SEARCH enables efficient code lookup operations.

Best Practices

Understanding best practices ensures effective use of the SEARCH statement.

Best Practices

  • Use SEARCH ALL for sorted tables - Improve performance with binary search
  • Handle AT END cases - Always provide AT END handling
  • Validate table bounds - Ensure table is properly defined
  • Use meaningful names - Choose descriptive table and variable names
  • Test thoroughly - Verify search behavior with various inputs

Test Your Knowledge

1. What is the primary purpose of the SEARCH statement in COBOL?

  • To search for files on disk
  • To search through tables and arrays
  • To search for text in strings
  • To search for records in files

2. What type of data structure does SEARCH work with?

  • Files only
  • Tables with OCCURS clause
  • Any data structure
  • Only numeric arrays

3. What happens when SEARCH finds a match?

  • The program stops
  • Control transfers to WHEN clause
  • The table is sorted
  • An error occurs

4. What happens when SEARCH does not find a match?

  • The program stops
  • Control transfers to AT END clause
  • The search continues
  • An error occurs

5. Can SEARCH be used with both sequential and binary search?

  • Yes, both types are supported
  • No, only sequential search
  • No, only binary search
  • It depends on the COBOL version

Frequently Asked Questions