|
From: Jim M. <ro...@vi...> - 2005-08-19 16:33:16
|
John You must realise,Little Grasshopper, that the Cobol EVALUATE is a statement of unimaginable power that outstrips the wimpy little case statements and switch seen in other languages. The EVALUATE TRUE form is one of many that are available under Cobol 85 that many people are not aware of. http://www.damos.dircon.co.uk/html/cobol_evaluate_verb.html We still get asked about using EVALUATE quite a lot, especially using multiple conditions on EVALUATE statements, so here are some quick pointers to using this statement. EVALUATE can be used in place of IF statements, it can often make the program more readable when you have complex nested conditions. In it's simplest form the EVALUATE statement goes something like this: EVALUATE condition WHEN value imperative-statements WHEN value imperative statements ........... END-EVALUATE. Here's a simple example: EVALUATE WS-X WHEN 1 ADD 15 TO WS-TOTAL PERFORM A-100-REPORT WHEN 2 ADD 16 TO WS-TOTAL MOVE 'WS-X IS 2' TO WS-DISPLAY PERFORM A-200-REPORT WHEN OTHER PERFORM X-100-ERROR END-EVALUATE. This will check the value of the variable WS-X and execute the statements depending on the value. Note the use of WHEN OTHER this will be executed if WS-X does not match any of the values, so in the example if WS-X is not equal to 1 or 2 then PERFORM X-100-ERROR will be executed. Sometimes you will want to have multiple conditions with lots of ANDs and ORs in an EVALUATE statement as you would in an IF statement. To do this with EVALUATE requires a slightly different approach. One way is to use EVALUATE TRUE (or EVALUATE FALSE). for example EVALUATE TRUE WHEN WS-X = 1 AND WS-Y = 2 PERFORM X-100-PROCESS1 WHEN WS-X =1 AND WS-Y NOT = 2 PERFORM X-200-PROCESS2 END-EVALUATE. Here, the whole condition on the WHEN statement is checked and if it is TRUE then the associated statement(s) are executed. The second way to do this is using EVALUATE ... ALSO. EVALUATE WS-AGE ALSO WS-SEX ALSO WS-WEIGHT WHEN 21 ALSO 'M' ALSO 150 PERFORM A-200-ACCEPT WHEN OTHER PERFORM A-300-DECLINE END-EVALUATE. In this example if WS-AGE is 21 AND WS-SEX is 'M' AND WS-WEIGHT is 150 then PERFORM A-200-ACCEPT is executed, if not then PERFORM A-300-DECLINE is executed. You can combine ALSO with the TRUE and FALSE conditions, so you could have EVALUATE TRUE ALSO FALSE for example. John R. Culleton wrote: >On Friday 19 August 2005 07:50 am, Jim Morcombe wrote: > > >>Here is another cut down listing. >> >>It demonstrates what I believe to be a bug in the compiler concerning >>the EVALUATE statement. >> >>When I compile it, I get the following error message: >> >>jim.cbl: 18: error: incompatible selection object, on or before '=' >> >>Jim >> >> IDENTIFICATION DIVISION. >> 2 000300 PROGRAM-ID. rgmssch. >> 3 001200 ENVIRONMENT DIVISION. >> 4 001700 >> 5 001900 DATA DIVISION. >> 6 002100 FILE SECTION. >> 7 002300 >> 8 002500 WORKING-STORAGE SECTION. >> 9 >> 10 01 LICENCE-DAYS PIC 9(5). >> 11 >> 12 >> 13 >> 14 019500 PROCEDURE DIVISION. >> 15 020200 MAINLINE. >> 16 >> 17 EVALUATE TRUE >> 18 WHEN LICENCE-DAYS = ZERO >> 19 CONTINUE >> 20 END-EVALUATE. >> >> > > >I am not familiar with such a construct. According to the books >I have, (three at the moment) you EVALUATE FIELDNAME. > >And then the following WHEN statements are equivalent to >IF FIELDNAME = (value) (executable statement) >ELSE IF.... > >The EVALUATE TRUE statement in your program does not refer to a field name >but a condition-name. This in turn requires a level 88 appended >to a field name in e.g., WORKING STORAGE. > >CONTINUE is used in an IF statement. I have not found it used in >a EVALUATE statement. > >TRUE is not a field but a condition name attributed to a field >that has an 88 statement like: > > O1 WS-ANOTHER-RECORD PICTURE X. > 88 ANOTHER-RECORD VALUE "Y" FALSE "N". > >EVALUATE with WHEN statements is an alternative to the use of IF with >condition >names. Don't mix the two. > > |