COBOL/400 Language Help

Format 2 Binary Search

Description
The Format 2 Binary Search statement executes a binary search. The search index does not need to be initialized by SET statements, because its setting is varied during the search operation so that its value is at no time less than the value of the first table element, nor ever greater than the value of the last table element. The index used is always that associated with the first index-name specified in the OCCURS clause.
Format 2 (Binary Search)
 
 ->->---SEARCH ALL identifier-1---*--------------------->1
                                *-*----*-------------->2
                                  *-AT-*
 
1->---------------------------------------*------------>
2->---END imperative-statement-1----------*
 
 ->---WHEN-*-data-name-1-*----*-*-EQUAL-*----*-*------->3
          +             *-IS-* +       *-TO-* +
          +                    *- = ----------*
          *-condition-name-1-------------------------->4
 
3->---*-identifier-3------------*---------------------->
     *-literal-1---------------*
     *-arithmetic-expression-1-*
4->-----------------------------*
 
    *------------------------------------------------<-5
    V
 ->--*------------------------------------------------->6
    *-AND--*-data-name-2-*----*-*-EQUAL-*----*-*------>7
           +             *-IS-* +       *-TO-* +
           +                    *- = ----------*
           *-condition-name-2------------------------->8
 
5<-------------------------------*
                                +
6->------------------------------*--------------------->
7->---*-identifier-4------------**
     *-literal-2---------------*+*-arithmetic-expression-2-*+
8->------------------------------*
 
 ->---*-imperative-statement-2---*------------*--*-----><-
     +                          *-END-SEARCH-*  +
     *-NEXT SENTENCE----------------------------*

identifier-1

Identifier-1 can be a data item subordinate to a data item that contains an OCCURS clause; that is, it can be a part of a multi-dimensional table. In this case, the data description entry must specify an INDEXED BY phrase for each dimension of the table.

Identifier-1 must refer to all occurrences within the table element; that is, it must not be subscripted or reference modified.

Identifier-1 can be an index data item.

identifier-1

Identifier-1 can be a data item subordinate to a data item that contains an OCCURS clause; that is, it can be a part of a two- to seven-dimensional table. In this case, the data description entry must specify an INDEXED BY phrase for each dimension of the table.

Before the search takes place, the values of all indexes should be set for higher dimensions of the table to define a specific table of identifier-1 elements.

Identifier-1 must refer to all occurrences within the table element; that is, it must not be subscripted or indexed.

Identifier-1 cannot be a pointer data item.

The Data Division description of identifier-1 must contain an OCCURS clause with the INDEXED BY option. It must contain the KEY IS phrase in its OCCURS clause.

In the WHEN phrase, if you specify an ASCENDING/DESCENDING KEY data item, explicitly or implicitly, you must also specify all preceding ASCENDING/DESCENDING KEY data-names for identifier-1.

identifier-2

Identifier-2 must be either an index data item or an elementary integer item.

index-name-1

If Index-name-1 is an index for identifier-1, this index is used for the search. Otherwise, the first (or only) index name is used. If index-name-1 is an index for another table element, then the first (or only) index-name for identifier-1 is used for the search. The occurrence number represented by index-name-1 is increased by the same amount as the search index-name and at the same time.

When you omit the VARYING index-name-1 phrase, the first (or only) index-name for identifier-1 is used for the search.

END imperative-statement-1

The at end condition exists when the search operation terminates without satisfying the condition specified in any of the associated WHEN phrases.

If the at end condition exists and the AT END phrase is specified, imperative-statement-1 is executed, and control passes to the end of the SEARCH statement, unless imperative-statement-1 ends with a GO TO statement.

If the at end condition exists and the AT END phrase is omitted, control passes to the next statement after the SEARCH statement.

imperative-statement-2

After imperative-statement-2 is executed, control passes to the end of the SEARCH statement, unless imperative-statement-2 ends with a GO TO statement.

condition-1

Condition-1 may be any conditional expression.

NEXT SENTENCE

If you specify the END-SEARCH phrase, you must not specify the NEXT SENTENCE phrase.

data-name-1 or -2

Data-name-1, data-name-2 must specify an ASCENDING/DESCENDING KEY data item in the identifier-1 table element and must be indexed by the first identifier-1 index-name, along with other indexes or literals, as required. Data-name-1, data-name-2 may be qualified.

identifier-3 or -4

Identifier-3 and identifier-4 must not be POINTER data items. They must not be ASCENDING/DESCENDING KEY data items for identifier-1 or an item that is indexed by the first index-name for identifier-1.

arithmetic-expression-1 or -2

In Format 2 of the SEARCH statement, arithmetic-expression-1 or arithmetic-expression-2 may be any arithmetic expression, with the following restriction: no identifier in the arithmetic expression may be an ASCENDING/DESCENDING KEY data item for identifier-1 or an item that is indexed by the first index-name for identifier-1.

Click here for a description of arithmetic expressions.

condition-name-1 or -2

Each condition-name specified must have only a single value, and each must be associated with an ASCENDING/DESCENDING KEY identifier for this table element.

END-SEARCH

The explicit scope terminator serves to delimit the scope of a statement. It permits the conditional form of the statement to be nested within another conditional statement. The explicit scope terminator may also be used with the imperative form of the statement.

The scope of a SEARCH statement may be terminated by any of the following:

If you specify the END-SEARCH phrase, you must not specify the NEXT SENTENCE phrase.


[ Top of Page | Previous Page | Next Page | Table of Contents ]