Order of evaluation of conditions in a combined condition 
Author Message
 Order of evaluation of conditions in a combined condition

Does the COBOL standard specify the order of evaluation of conditions
and whether evaluation always stops as soon as the answer can be
determined.

e.g. Could the following give 'Subscript Out Of Range' or 'Out of
Bounds' type errors, when WS-SUB is 101 or greater?

  01  WS-ENTRY    PIC X(4) OCCURS 100.

  IF ( WS-SUB > 100 )
  OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
     GO TO A-900-END-OF-LOOP.

I know what the compiler I currently use does, but I am interested in
what the standards say.

John Plumbley




Sun, 13 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Quote:

>Does the COBOL standard specify the order of evaluation of conditions
>and whether evaluation always stops as soon as the answer can be
>determined.

>e.g. Could the following give 'Subscript Out Of Range' or 'Out of
>Bounds' type errors, when WS-SUB is 101 or greater?

>  01  WS-ENTRY    PIC X(4) OCCURS 100.

>  IF ( WS-SUB > 100 )
>  OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
>     GO TO A-900-END-OF-LOOP.

>I know what the compiler I currently use does, but I am interested in
>what the standards say.

>John Plumbley



There was an interpretation related to this.  (I think it had to do with
EVALUATE - but was still relevant).  My memory is that the interpretation
did say that you could stop evaluating after the first "false" in an "AND"
type statement.

All the J4 people are in a meeting this week, so I can't get you a
definitive answer this week - but they should be able to help next week
(unless someone has a handy copy of the interpretation and wants to  give
the answer now).

--
+ +
+   Bill Klein -
         "C" is a nice letter to START the name of your programming language
with
      but I wouldn't want to end up there.



Sun, 13 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Hmmm... interesting question there.

I know that when I was doing Pascal (don't ask!), the Borland compiler gave
me the option of setting the short-circuit. I'm not sure about COBOL/400 or
VAX/COBOL though... I don't recall ever encountering any such directive or
compiler option being available.

Just to be on the safe side, I always coded around the problem, though.
Required a few extra lines of logic, but it ensured that I'd never run into
an out-of-bounds condition.

Quote:


>>Does the COBOL standard specify the order of evaluation of conditions
>>and whether evaluation always stops as soon as the answer can be
>>determined.

>>e.g. Could the following give 'Subscript Out Of Range' or 'Out of
>>Bounds' type errors, when WS-SUB is 101 or greater?

>>  01  WS-ENTRY    PIC X(4) OCCURS 100.

>>  IF ( WS-SUB > 100 )
>>  OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
>>     GO TO A-900-END-OF-LOOP.

>>I know what the compiler I currently use does, but I am interested in
>>what the standards say.

>>John Plumbley


>There was an interpretation related to this.  (I think it had to do with
>EVALUATE - but was still relevant).  My memory is that the interpretation
>did say that you could stop evaluating after the first "false" in an "AND"
>type statement.

>All the J4 people are in a meeting this week, so I can't get you a
>definitive answer this week - but they should be able to help next week
>(unless someone has a handy copy of the interpretation and wants to  give
>the answer now).

>--
>+ +
>+   Bill Klein -
>         "C" is a nice letter to START the name of your programming
language
>with
>      but I wouldn't want to end up there.



Mon, 14 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

: Does the COBOL standard specify the order of evaluation of conditions
: and whether evaluation always stops as soon as the answer can be
: determined.

In COBOL 1985 this is addressed in section 6.3.4, Order of
Evaluation of Conditions, pp. VI-61 through VI-66.  I won't quote
the whole thing, but I think this answers your question:

     The constituent connected conditions within a hierarchical
     level are evaluated in order from left to right, and evaluation
     of that hierarchical level terminates as soon as a truth value
     for it is determined regardless of whether all the consituent
     connected conditions within that hierarchical level have been
     evaluated.

In other words, evaluate left-to-right and stop as soon as possible.
The Standard contains flowcharts illustrating these rules for several
specific examples of complex conditions.

I hope this helps, and I applaud anyone who actually cares what
the Standard says, rather than just the behavior of a particular
compiler.

Walter Murray



Mon, 14 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Quote:

> Does the COBOL standard specify the order of evaluation of conditions
> and whether evaluation always stops as soon as the answer can be
> determined.

> e.g. Could the following give 'Subscript Out Of Range' or 'Out of
> Bounds' type errors, when WS-SUB is 101 or greater?

>   01  WS-ENTRY    PIC X(4) OCCURS 100.

>   IF ( WS-SUB > 100 )
>   OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
>      GO TO A-900-END-OF-LOOP.

> I know what the compiler I currently use does, but I am interested in
> what the standards say.

I don't know, but I sure hope it's that way in the standard (short-circuit
logic, that is).  In our shop we have hundreds of conditional that look like the
following:

     IF WS-VALUE NUMERIC
             AND WS-VALUE > 0
       ....

If WS-VALUE was not numeric, and it tried to evaluate the second condition,
all of our programs would blow up!  :-)

--



Mon, 14 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

John,

AFAIK, if you're looping using an In-Line Perform and you default to 'TEST
BEFORE', you're actually executing a 'DO WHILE'. If WS-SUB had an initial
value of 101, the loop would not even be executed. However, if you specify
'TEST AFTER' (DO UNTIL), you could go out of bounds. In other words, you
are guaranteed that the 'TEST AFTER' will execute at least once whereas the
'TEST BEFORE' may not execute at all. Therefore, the Syntax 'UNTIL WS-SUB
IS GREATER THAN 100' will not bomb when using the 'TEST BEFORE' default.

Your code should be changed to protect a boundary error on the OR by
ensuring that when you're looking for HIGH-VALUES, WS-SUB IS NOT GREATER
THAN 100.

This is just my opinion to prevent the always ugly 3AM call....

Cheers,

WOB



Quote:
> Does the COBOL standard specify the order of evaluation of conditions
> and whether evaluation always stops as soon as the answer can be
> determined.

> e.g. Could the following give 'Subscript Out Of Range' or 'Out of
> Bounds' type errors, when WS-SUB is 101 or greater?

>   01  WS-ENTRY    PIC X(4) OCCURS 100.

>   IF ( WS-SUB > 100 )
>   OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
>      GO TO A-900-END-OF-LOOP.

> I know what the compiler I currently use does, but I am interested in
> what the standards say.

> John Plumbley





Tue, 15 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Frank,

Your code is fine. Probably, a TRT is done for the test for numerics. If it
failed then your greater than zero test would never be executed.

John's code has the potential for failure due to its lacking of testing
WS-SUB being out of range. I had suggested to him that WS-SUB should be
tested for LESS THAN 101 before attempting to test for HIGH-VALUES.

Cheers,

WOB



Quote:

> > Does the COBOL standard specify the order of evaluation of conditions
> > and whether evaluation always stops as soon as the answer can be
> > determined.

> > e.g. Could the following give 'Subscript Out Of Range' or 'Out of
> > Bounds' type errors, when WS-SUB is 101 or greater?

> >   01  WS-ENTRY    PIC X(4) OCCURS 100.

> >   IF ( WS-SUB > 100 )
> >   OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
> >      GO TO A-900-END-OF-LOOP.

> > I know what the compiler I currently use does, but I am interested in
> > what the standards say.

> I don't know, but I sure hope it's that way in the standard
(short-circuit
> logic, that is).  In our shop we have hundreds of conditional that look
like the
> following:

>      IF WS-VALUE NUMERIC
>              AND WS-VALUE > 0
>        ....

> If WS-VALUE was not numeric, and it tried to evaluate the second
condition,
> all of our programs would blow up!  :-)

> --



- Show quoted text -



Tue, 15 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Quote:


>> Does the COBOL standard specify the order of evaluation of conditions
>> and whether evaluation always stops as soon as the answer can be
>> determined.

>> e.g. Could the following give 'Subscript Out Of Range' or 'Out of
>> Bounds' type errors, when WS-SUB is 101 or greater?

>>   01  WS-ENTRY    PIC X(4) OCCURS 100.

>>   IF ( WS-SUB > 100 )
>>   OR ( WS-ENTRY(WS-SUB) = HIGH-VALUES )
>>      GO TO A-900-END-OF-LOOP.

>> I know what the compiler I currently use does, but I am interested in
>> what the standards say.

>I don't know, but I sure hope it's that way in the standard (short-circuit
>logic, that is).  In our shop we have hundreds of conditional that look
like the
>following:

>     IF WS-VALUE NUMERIC
>             AND WS-VALUE > 0
>       ....

>If WS-VALUE was not numeric, and it tried to evaluate the second condition,
>all of our programs would blow up!  :-)

>--



I am still working from memory, but I think that the code that you have is
EXACTLY the type of code that is NOT guaranteed to work (by the Standard)
but that does work with most compilers.

--
+ +
+   Bill Klein -
         "C" is a nice letter to START the name of your programming language
with
      but I wouldn't want to end up there.



Wed, 16 Aug 2000 03:00:00 GMT  
 Order of evaluation of conditions in a combined condition

Quote:

> I am still working from memory, but I think that the code that you have is
> EXACTLY the type of code that is NOT guaranteed to work (by the Standard)
> but that does work with most compilers.

No, William, sorry for you, but:

It is as Walter Murray wrote in another msg in this thread: the standard order
is left-to-right. So the standard DOES GUARANTEE this to work! It was in the
Standard in '74, in '85 and will stay in the new (2000?). But I do agree with
other tips in this thread to code a safe-guard test: be sure and double sure. In
my opinion is works best when you code as if the order of evaluation is NOT
guaranteed.

BTW: Nice work on that COBOL FAQ!

Huib.



Fri, 18 Aug 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Order of evaluation of conditions in a combined condition/PMAP

2. Splitting 'and' conditions into multiple conditions

3. Pre-condition vs. Post-condition

4. Pre-condition vs. Post-condition

5. Pre-condition vs Post-condition

6. "BIG E vs little e", was pre-condition vs post-condition

7. pre-condition vs post-condition

8. How to combine more conditions?

9. Efficient evaluation of multiple conditions within IF statements

10. Normal Order Evaluation Vs.Applicative Order Evaluation?

11. Order of class invariant and routine pre-condition evaluation ?

12. Order of class invariant and routine pre-condition evaluation ?

 

 
Powered by phpBB® Forum Software