Mainframe Forum: A comprehensive repository for programming tutorials and technology news. Got a minute? Click upon those blue words to start learning in Cobol, DB2, CICS, JCL, CA7, APIs, DevOps, Agile, JAVA, SORT, Excel macro, python, and mainframe tools.
Welcome back to today's session on "COBOL Evaluate Statement". In this session, we will precisely explain the "Evaluate Statement in COBOL" and deep dive into the different forms of COBOL Evaluate statements. You'll go through different Evaluate statement examples for better understanding. So, let's get started with an introduction to COBOL Evaluate Statement.
COBOL Evaluate Statement - Agenda.
Introduction to Evaluate Statement in COBOL.
How and when to use Evaluate in COBOL.
Different forms of COBOL Evaluate Statement.
COBOL Evaluate Statement Example.
COBOL Evaluate Statement Tutorial.
Conclusion.
Introduction.
EVALUATE in COBOLis similar to Microsoft Visual Basic "Select Case Statement". COBOL Evaluate statement is easy to implement and improve code readability. The key difference between EVALUATE and VBA Select Case is that no ‘Break’ is required i.e. control automatically transpire of the EVALUATE statement as soon as condition matched.
You can use Nested Evaluate in COBOL instead of nested If-ELSE statements.
How and when to use the COBOL Evaluate statement?
COBOL EVALUATE clause is better than nested IF-ELSE statement in terms of performance it saves a lot of CPU and simplifies the program logic. You can use nested IFs statements to implement business logic. There is no end to the depth of COBOL nested IF statements. But, when the COBOL program has to examine a variable for more than two levels, EVALUATE is the more preferred choice.
Evaluate statement in COBOL is faster than nested IF-ELSE statements. It is not possible to replace all nested-Ifs in the program but you should try to replace as much as you can.
Evaluate in COBOL Syntax.
Let me write down the syntax of the COBOL EVALUATE statement.
* The syntax of the Evaluate statement as an If-statement alternative.
EVALUATE TRUE WHEN Condition-1 Statement ... WHENOTHER Statement- ... END-EVALUATE.
During the execution of a COBOL EVALUATE statement, the values denoted by the list of subjects (items in the EVALUATE statement in COBOL) are compared with the values denoted by the list of objects in a WHEN phrase to establish a “match” between the two.
The value of a subject is compared with the value/range of values of the object in the corresponding ordinal position In the case of a single-valued (numeric/non-numeric) object, the subject-object comparison is done in the usual way.
When a range of values is specified for the object, the subject-object comparison results in TRUE, if the value of the subject falls within the range In the case of conditional values, the subject-object comparison results in TRUE, if both evaluate to the same value (that is if both are TRUE or both are FALSE).
Different forms of COBOL Evaluate Statement.
COBOL Evaluate statement has multiple formats. These formats suit the need of the programmer and enable them to implement business logic seamlessly. Let's deep dive into different forms of COBOL Evaluate Statements.
#1. COBOL EVALUATE WHEN TRUE.
In this example, you'll see how to use multiple when conditions (i.e. COBOL evaluate multiple when).
EVALUATE TRUE WHEN END-OF-THE-FILE PERFORM A00-WRT-TRL-REC WHEN NOT-END-OF-THE-FILE PERFORM B00-WRT-DTL-REC PERFORM B10-RED-NXT-REC WHEN OTHER PERFORMZ00-CLL-ERR End-EVALUATE
END-EVALUATE.
#2. COBOL EVALUATE WHEN TRUE ALSO TRUE.
The following example explains how you can code COBOL EVALUATE WHEN TRUE ALSO TRUE conditions (i.e. COBOL evaluate condition).
EVALUATE TRUE ALSO TRUE WHEN WS-A = WS-B ALSO WS-C = WS-D DISPLAY 'CASE 1 is True' WHENWS-A > WS-B ALSO WS-C < WS-D DISPLAY 'CASE 2 is True' WHEN OTHER DISPLAY 'OTHER CASE is True'
END-EVALUATE.
#3. COBOL FORMAT 3: EVALUATE .... WHEN with multiple conditions i.e. AND and OR operator.
The following example explains how you can code COBOL EVALUATE WHEN with multiple conditions with logical operators (i.e. COBOL evaluate condition).
EVALUATE MARTIAL-STATUS WHEN 'M' DISPLAY 'Married' WHEN 'S' WHEN 'D' WHEN 'W' DISPLAY 'Single' WHEN OTHER DISPLAY 'Not Specified'
END-EVALUATE.
EVALUATE TRUE
WHEN WS-AGE > 10 AND WS-GEN = 'M'
DISPLAY 'MALE with Age > 10'
WHEN WS-AGE > 10 AND WS-GEN = 'F'
DISPLAY 'Female with Age > 10'
WHEN (WS-AGE > 10 OR WS-AGE < 15)AND WS-GEN = 'F' DISPLAY 'Female with Age between 10 - 15'
WHEN OTHER
DISPLAY 'Criteria not satisfy'
END-EVALUATE.
#4 COBOL EVALUATE using THRU Phrase (i.e. EVALUATE WHEN with THRU).
This example teaches how you can code different conditions in a series of values to lead to the corresponding processing action by coding the THRU phrase. Operands in a COBOL EVALUATE THRU phrase must be of the same class.
EVALUATE WS-NUMBER WHEN 1 DISPLAY '1' WHEN 2 DISPLAY '2' WHEN 3 THRU 6 DISPLAY 'Number between 3 - 6' WHEN OTHER DISPLAY 'NUMBER not in range' END-EVALUATE.
If ANY is specified for an object, the subject-object comparison always results in TRUE
The list of subjects is said to “match” with the list of the object if all the corresponding subject-object comparisons result in true
Note: that the values of the subjects need not be of the same class. For example, one can be numeric and the other can be alphanumeric After the execution of one of the when clauses, the control is automatically passed on to the next sentence after the EVALUATE statement.
COBOL EVALUATE Statement - Youtube.
Conclusion.
Finally, this marks an end to the COBOL Evaluate Statement. COBOL Evaluate statement goes hand in hand and you got an alternative to COBOL Ifs statements. Evaluate statement in COBOL is better than IFs statement in terms of performance. Do check out COBOL LEVEL 88 Condition.
►Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on -
Enable GingerCannot connect to Ginger Check your internet connection or reload the browserDisable in this text fieldRephraseRephrase current sentenceEdit in Ginger×
Enable GingerCannot connect to Ginger Check your internet connection or reload the browserDisable in this text fieldRephraseRephrase current sentenceEdit in Ginger×
A batch program reads an account record and must choose one path: accept it, reject it, route it to review, or skip it because the file reached end. COBOL decision making is the set of statements that makes that choice visible in the program: IF, EVALUATE, and PERFORM.
Pick the clear branch.
What is decision making in COBOL?
Decision making means testing data and running the matching statements. In a mainframe program, the tested data might be a file status, transaction code, account type, return code, amount, date, or user-selected action.
IBM groups IF and EVALUATE as statements for selecting program actions. Use them with explicit scope terminators such as END-IF and END-EVALUATE so the next developer can see where each decision ends.
When to use IF
Use IF when the program has one condition or two simple choices. IBM describes IF ... ELSE as the normal form for choosing between two processing actions. The word THEN is optional in COBOL, but many teams omit it for a cleaner style.
IF WS-FILE-STATUS = '00'
PERFORM PROCESS-CUSTOMER
ELSE
PERFORM WRITE-FILE-ERROR
END-IF
This is direct and easy to test. The branch names also tell the reader what business action happens, not only what low-level condition was true.
When to use EVALUATE
Use EVALUATE when there are three or more choices. IBM describes EVALUATE as a way to avoid nested IF statements and to code a case structure or decision table.
EVALUATE WS-ACTION-CODE
WHEN 'A'
PERFORM ADD-CUSTOMER
WHEN 'C'
PERFORM CHANGE-CUSTOMER
WHEN 'D'
PERFORM DELETE-CUSTOMER
WHEN OTHER
PERFORM WRITE-INVALID-ACTION
END-EVALUATE
WHEN OTHER should handle values that do not match a known action. It is also a good place to write a clear message, set a return code, or route the record to an error report.
IF vs EVALUATE at a glance
Need
Best COBOL statement
Reason
One test with one action
IF
Smallest clear form.
Two alternate actions
IF ... ELSE
Shows both paths without extra structure.
Menu code, status code, or many values
EVALUATE
Avoids long nested IF blocks.
Several conditions together
EVALUATE TRUE or carefully written IF
Keeps rule order visible.
Using EVALUATE TRUE for business rules
EVALUATE TRUE is useful when each WHEN contains a full condition. This reads like a rule list. Put the most specific rules first so a broad rule does not catch the record too early.
EVALUATE TRUE
WHEN WS-FILE-STATUS NOT = '00'
PERFORM WRITE-FILE-ERROR
WHEN WS-ACCOUNT-BALANCE < ZERO
PERFORM ROUTE-CREDIT-REVIEW
WHEN WS-CUSTOMER-TYPE = 'VIP'
PERFORM PROCESS-VIP-CUSTOMER
WHEN OTHER
PERFORM PROCESS-STANDARD-CUSTOMER
END-EVALUATE
IBM notes that WHEN phrases are tested in source order. That order is part of the program logic, so treat it like business code rather than formatting.
Where PERFORM fits
PERFORM does not choose by itself. It runs a paragraph, section, or inline block after a choice has already been made. A clear decision block often calls short named paragraphs with PERFORM.
IF WS-INPUT-VALID
PERFORM UPDATE-CUSTOMER
ELSE
PERFORM PRINT-REJECT-DETAIL
END-IF
That style keeps the decision near the data test and moves longer processing into named routines. IBM also recommends structured programming statements such as EVALUATE and inline PERFORM because they make control flow easier to follow.
Avoid hidden period bugs
Old COBOL often used periods to end scope. Modern COBOL is easier to review when each decision uses explicit endings. A period in the wrong place can end more than the reader expects.
IF WS-FOUND
PERFORM PRINT-DETAIL
END-IF
PERFORM READ-NEXT-RECORD
Use END-IF, END-EVALUATE, END-PERFORM, END-READ, and similar scope terminators. The compiler accepts older styles, but a support team needs code that can be read quickly during an abend call.
CONTINUE vs NEXT SENTENCE
CONTINUE is a no-operation statement. Control moves to the next statement after the current scope. NEXT SENTENCE jumps to the statement after the next period. IBM warns that the two can behave very differently depending on where the next period appears.
IF WS-SKIP-RECORD
CONTINUE
ELSE
PERFORM PROCESS-RECORD
END-IF
Prefer CONTINUE when you need an empty branch. Avoid NEXT SENTENCE in new code unless your site standard has a very specific reason for it.
Common mistakes
Nesting too many IF statements
Three or four nested IF statements are easy to misread. If the code checks a menu value, status code, or action code, rewrite it as EVALUATE.
Forgetting WHEN OTHER
A new action code can arrive from a file or screen before the program is ready for it. WHEN OTHER gives the program a controlled reject path instead of silent fall-through.
Mixing decisions and long processing
A decision block should show the choice. Long update logic, reporting logic, and error handling usually belong in named paragraphs called by PERFORM.
Review checklist
Use IF for one or two choices.
Use EVALUATE for action codes, file statuses, menu choices, and decision tables.
Code WHEN OTHER for unexpected values.
Use explicit scope terminators such as END-IF and END-EVALUATE.
Keep branch bodies short and call named paragraphs with PERFORM.
Prefer CONTINUE over NEXT SENTENCE for empty branches.