COBOL Evaluate Verb

COBOL IF Conditional Expression COBOL Iterative Programming

Advertisements


EVALUATE provides a multi selection control during the program execution.

EVALUATE used to validate multiple conditions in a single shot.

EVALUATE statement in COBOL is similar to Case or Switch statements of other languages.

EVALUATE can do multiple IF conditions task.

If any EVALUATE WHEN conditions satisfies, the list of statements will be executed under the WHEN and control will transfers to the next executable statement after ending of EVALUATE.

If none of the EVALUATE satisfies, by default the statements coded under WHEN OTHER will be executed and control transfers to the next executable statement after ending of EVALUATE.

END-EVALUATE is optional with EVALUATE.


Syntax:



Simple Syntax:

			
EVALUATE {Expression1}
	WHEN Cond-1
		Statement block-1
	WHEN Cond-2
		Statement block-2
		.
		.
		.
		.
	WHEN Cond-n
		Statement block-n
	WHEN other
		Statement block- other
END-EVALUATE.

EVALUATE Working:


Normally, the expression along with the EVALUATE statement is called as the subject.

The conditions provided along with the WHEN Clause are called as the object.

By using the evaluate statement, programmer can test the subject expression against an object value.

The EVALUATE Block allows programmer to specify the multiple subjects and objects like the First syntax provided above.

Each subject in the EVALUATE Clause will be tested by using the corresponding object-value in the WHEN Clause.

If none of the WHEN clauses can be chosen and a WHEN OTHER clause coded in the EVALUATE, the WHEN OTHER clause will be executed regardless of the Condition.

WHEN OTHER clause will execute for all other conditions which are not coded in WHEN clause.


EVALUATE Types:


EVALAUTE can be divided into below types based on its usage in the program.


  1. Simple EVALUATE
  2. EVALUATE TRUE
  3. EVALAUTE with THRU
  4. EVALUATE with multiple WHEN conditions
  5. EVALUATE with MULTIPLE conditions

Simple EVALUATE:


In this, EVALUATE will have only one condition to validate.

EVALUATE validates the data-item and WHEN condition will have the values to validate the data-item.


Practical Example:

Code:



The above simple program is used to receive the current day number in week and displays what is today.

EVALUATE validates the WEEK-DAY with all WHEN conditions which have values.

Based on the number entered, the corresponding WHEN condition will be executed.


Jcl:



Output:



EVALUATE TRUE:


In this, EVALUATE TRUE will have only one condition to validate.

EVALUATE check all the conditions in WHEN clause to get the outcome of condition as TRUE.

EVALUATE starts validating from the first WHEN, any WHEN satisfies the corresponding statements will be executed and the control transfers to the next statement after the END-EVAUATE.


Practical Example:

Code:



EVALUATE TRUE works in the same way how the simple EVALUATE working but the condition always with WHEN instead of EVALUATE.


Jcl:



Output:



EVALUATE with THRU:


EVALUATE THRU used to validate the set of values which are contiguous.

EVALUATE THRU works as simple EVALUATE but set of values in WHEN from one value THRU other value.

EVALUATE will start validating from the first WHEN, any WHEN satisfies the corresponding statements will be executed and the control transfers to the next executable statement after the END-EVAUATE.


Practical Example:

Code:



Jcl:



Output:



EVALUATE using multiple WHEN phrases:


EVALUATE with multiple WHEN phases used to validate the set of values which needs to have same action for different WHEN (different values).

EVALUATE with multiple WHEN phases, works with multiple WHEN and each WHEN have a different value.

If no statements added to WHEN phase, then EVALUATE treats that next WHEN is of same category.

It will continue up to the WHEN which has the statements under it. All those WHEN’s combined as single WHEN with multiple values which are not contiguous.

EVALUATE will start validating from the first WHEN, any WHEN satisfies the corresponding statements will be executed and the control transfers to the next statement after the END-EVAUATE.


Practical Example:

Code:



In the above example, program receives the student grade as input and displays the student class.

Grades A, B, C and D are FIRST CLASS. Grade E is SECOND CLASS. Grades F and G is THIRD CLASS. For all other Grades then student got failed.

So our requirement is for A, B, C and D we need to display as FIRST CLASS.

The EVALUATE can be coded the like below.


EVALUATE STD-CODE
	WHEN ‘A’
	WHEN ‘B’
	WHEN ‘C’
	WHEN ‘D’
		DISPLAY ‘STUDENT GOT FIRST CLASS’

If user enters any of the above grades, then STUDENT GOT FIRST CLASS will be displayed.


Jcl:



Output:



EVALUATE with Multiple Conditions:


EVALUATE with multiple conditions used to validate the set of conditions which are combining with ALSO have same action.

EVALUATE with multiple conditions works with multiple conditions having different values for each condition.

EVALUATE will start validating from the first WHEN, any WHEN satisfies the corresponding statements will be executed and the control transfers to the next statement after the END-EVAUATE.

By using ALSO, user can more than one condition to WHEN clause.


Practical Example:

Code:



Jcl:



Output:



COBOL IF Conditional Expression COBOL Iterative Programming

Advertisements