Evaluate in COBOL

Like other programming languages in COBOL also we have EVALUATE. It is similar like SELECT in C or PHP.

We can avoid multiple IF or nested IF (which are very confusing) using EVALUATE.
Example:



EVALUATE variable1
    WHEN 1
    WHEN 3
        MOVE 'LOAN'      TO variable2
    WHEN 2
        MOVE 'CREDIT’    TO variable2
    WHEN 4
        MOVE 'DEBIT’     TO variable2
    OTHER
        MOVE ‘INVALID’   TO variable2
END-EVALUATE

In above example if variable1 is 1 OR 3, variable2 will be ‘LOAN’, if variable1 is 2, variable2 will be ‘CREDIT’, if variable1 is 4, variable2 will be ‘DEBIT’ and any of these are not true variable2 will be ‘INVALID’.

Above is the evaluate with OR condition, below example will elaborate AND condition in evaluate.

In IF we can write this:


If VAR1=”A” AND VAR2=”B”
    DISPLAY “TRUE”
ELSE
    IF VAR1=”C” AND VAR2=”D”
        DISPLAY “FALSE”
    ELSE
        DISPLAY “INVALID”
    END-IF

Instead of above nested and complex if condition (which could be more complex with more conditions) we can convert this logic to a simple EVALUATE logic.


EVALUATE VAR1 ALSO VAR2
    WHEN ”A” ALSO ”B”
        DISPLAY “TRUE”
    WHEN “C” ALSO “D”
        DISPLAY “FALSE”
    OTHER
        DISPLAY “INVALID”
END-EVALUATE

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Like other programming languages in COBOL also we have EVALUATE. It is similar like SELECT in C or PHP. We can avoid multiple IF or nested IF (which are very confusing) using EVALUATE. Example: EVALUATE variable1 WHEN 1 WHEN 3 MOVE ‘LOAN’      TO variable2 WHEN 2 MOVE ‘CREDIT’ TO variable2 WHEN 4 MOVE ‘DEBIT’ TO …

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Review Overview

User Rating: 4.03 ( 2 votes)
0

Leave a Reply