Summary -

In this topic, we described about the below sections -

The SET statement is used to perform below operations -

  1. Initializing the indexes of the table.
  2. Incrementing or decrementing an occurrence number.
  3. Setting the status of an external switch to ON or OFF.
  4. Moving data to condition names to make conditions true.

SET for Initializing Indexes

When the SET statement is executed, the current value of the receiving field is replaced by the value of the sending field.The value used for the sending field is the value at the beginning of SET statement execution.

The value of an index after execution of a SEARCH or PERFORM statement can be undefined; therefore, using SET statement to reinitialize such indexes before attempting other table-handling operations.

Syntax -

SET Syntax for Initializing Indexes

index-name-1

Receiving field. Specifies an index name in the INDEXED BY phrase of an OCCURS clause.

identifier-1

Receiving field. Specifies either an index data item or an elementary numeric integer item. Receiving field cannot be a date field.

index-name-2

Sending field. Specifies an index name in the INDEXED BY phrase of an OCCURS clause. The index value must correspond to an occurrence number of its associated table.

identifier-2

Sending field. Specifies either an index data item or an elementary numeric integer item. A sending field cannot be a date field.

integer-1

Sending field. Must be a positive integer.

Example -

To calculate student percentage by taking 6 subjects marks as input.


Code:


IDENTIFICATION DIVISION.  
PROGRAM-ID. ARRYINDX.  
ENVIRONMENT DIVISION.  
DATA DIVISION.       
WORKING-STORAGE SECTION.  
01 STD-DET OCCURES 6 TIMES INDEXED BY STD-INDEX.  
   05 STD-MARKS              PIC 9(03).           
01 TOTAL-MARKS               PIC 9(03) VALUE ZERO.
01 STD-PERCENT               PIC 9(03).9(02).    
01 I                         PIC 9(01).        
PROCEDURE DIVISION.                       
    MOVE ZEROES     TO TOTAL-MARKS.       
    PERFORM VARYING I FROM 1 BY 1         
      UNTIL I > 6                         
       SET STD-INDEX TO 1                 
       ACCEPT STD-MARKS (STD-INDEX)       
       ADD STD-MARKS (STD-INDEX) TO TOTAL-MARKS   
       SET STD-INDEX UP BY 1                
    END-PERFORM.                           
    COMPUTE STD-PERCENT = TOTAL-MARKS/6.   
    DISPLAY 'STUDENT PERCENTAGE : ' STD-PERCENT.  
    STOP RUN.               
           

SET for adjusting indexes -

When the SET statement is executed, the value of the receiving index is increased (UP BY) or decreased (DOWN BY) by a value in the sending field.

Syntax - SET (adjusting indexes)

SET Syntax for Adjusting Indexes

The receiving field is an index specified by index-name-3. Before and after the SET statement execution, the index value must correspond to an occurrence number in an associated table.

The sending field (identifier-3) must be an elementary integer data item or integer-2 which must be a nonzero integer. identifier-3 cannot be a date field. When the format-2 SET statement is executed, the contents of the receiving field are increased (UP BY) by identifier-3 or decreased (DOWN BY) by integer-2.

Example -

To calculate student percentage by taking 6 subjects marks as input.

Code:


IDENTIFICATION DIVISION.                                       
PROGRAM-ID. PERFTIMI.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
01 STD-DET OCCURES 6 TIMES INDEXED BY STD-INDEX.               
   05 STD-MARKS              PIC 9(03).                         
01 TOTAL-MARKS               PIC 9(03) VALUE ZERO.              
01 STD-PERCENT               PIC 9(03).9(02).                   
01 I                         PIC 9(01).                         
PROCEDURE DIVISION.                                            
    MOVE ZEROES     TO TOTAL-MARKS.                             
    PERFORM VARYING I FROM 6 BY 1   
      UNTIL I = 0                                               
       SET STD-INDEX TO 6                                       
       ACCEPT STD-MARKS (STD-INDEX)                             
       ADD STD-MARKS (STD-INDEX) TO TOTAL-MARKS                 
       SET STD-INDEX DOWN BY 1                                  
    END-PERFORM.                                                
    COMPUTE STD-PERCENT = TOTAL-MARKS/6.                        
    DISPLAY 'STUDENT PERCENTAGE : ' STD-PERCENT.                
    STOP RUN.                                                   

SET for external switches -

When the SET statement is executed, the status of each external switch associated with mnemonic-name is turned ON or OFF.

Syntax -

SET Syntax for External Switches

mnemonic-name-1

Must be associated with an external switch, the status of which can be altered.

SET for condition-names -

When the SET statement is executed, the condition-name value is placed in its conditional variable according to the rules of the VALUE clause.

Syntax - SET (condition-names)

SET Syntax for Condition Names

condition-name-1

Must be associated with a conditional variable. If more than one literal is specified in the VALUE clause of condition-name-1, the first literal is set to its conditional variable.

Example:

To validate the student marks entered as input.

Code:

SET for Conditional Names Code

In the above example, "SET STD-MARKS-VALID TO TRUE" will set STD-MARKS-SW to 'Y'

Output:

SET for Conditional Names Output