Page 1 of 1

Regarding the Logic

PostPosted: Fri Feb 01, 2013 8:46 pm
by vinu78
Hi Experts,

Whenever Code is between 1-20 or 51-70, we are executing some business logic.
My requirement is that, whenever Item 500 with Code 30-40 comes, pass through this same logic.
I tried adding this logic but the problem is that whenever Item is 500 having Code between 1-20, that also gets passed through this business logic which I don't want.
Note: Item 500 only with Code 30-40 should be passed through this logic and other codes for this Item 500 should be bypassing this logic.


IF (CODE > 0  AND < 21) OR
   (CODE > 50 AND < 71) OR
   ((ITEM = 500) AND (CODE > 29 AND < 41))    <= Newly inserted code
   < Process business logics and write to file>
END-IF


Please help.

Thanks
Vinu

Re: Regarding the Logic

PostPosted: Fri Feb 01, 2013 9:00 pm
by enrico-sorichetti
add an ( ITEM <not equal> 500 ) to the first two conditions

Re: Regarding the Logic

PostPosted: Fri Feb 01, 2013 9:25 pm
by Robert Sample
vinu78, you need to spend more time understanding how COBOL conditionals work. For the OR, if the first part is TRUE then COBOL does not evaluate the second part of the condition. So in your IF statement, if CODE has the value 20 then the first part is evaluated as TRUE and the second and third parts are not evaluated. This is why you are getting the results you see, and why enrico's suggestion will fix your problem.

Re: Regarding the Logic

PostPosted: Fri Feb 01, 2013 9:26 pm
by vinu78
Thanks Enrico.

Actually the full code is

IF (CODE > 0  AND < 21) OR
   (CODE > 50 AND < 71) OR
   (CODE > 85AND < 90) OR
   (CODE > 95 AND < 100) OR
   ((ITEM = 500) AND (CODE > 29 AND < 41))    <= Newly inserted code
   < Process business logics and write to file>
END-IF


I just mentioned a part of it for easy understandable scenario. So whether I need to mention ITEM Not equal 500 in the first 4 conditons ?

Thanks
Vinu

Re: Regarding the Logic

PostPosted: Fri Feb 01, 2013 9:49 pm
by Robert Sample
IF  (ITEM = 500 AND CODE > 29 AND CODE < 41) OR
    (ITEM NOT = 500 AND
        ((CODE > 0  AND < 21) OR
         (CODE > 50 AND < 71) OR
         (CODE > 85 AND < 90) OR
         (CODE > 95 AND < 100)))
might be a little easier to follow and maintain. When mixing AND and OR conditions, use parentheses to make clear the order but don't add parentheses just for the fun of it. The first conditional is AND AND which doesn't need any parentheses to make clear how it will be evaluated.

Re: Regarding the Logic

PostPosted: Sat Feb 02, 2013 9:37 pm
by BillyBoyo
I feel parentheses and indentation make things clearer.

           IF  ( ITEM = 500
               AND ( ACODE > 29 )
                   AND ( ACODE < 41 ) )
           OR  ( ( ITEM NOT = 500 )
               AND (  ( ACODE > 0 AND < 21 )
                   OR ( ACODE > 50 AND < 71 )
                   OR ( ACODE > 85 AND < 90 )
                   OR ( ACODE > 95 AND < 100 ) ) )
              stuff
           END-IF


I do even do that "inner AND". As an AND, as Robert says, it will work exactly without the parentheses, but, and although it won't happen in this example, with a small change to the test, and then the need to change the AND to an OR, things go awry.

IF (ITEM = 500 AND ACODE = 29 OR ACODE = 41)
    stuff
END-IF


Without parentheses that operates as ITEM = 500 and ACODE = 29, OR, entirely separately and on its own, nothing to do with ITEM, ACODE = 41.

Better anyway with 88s.

Here are the data definitions and 88s I invented. Note how the 88s give descriptive power to the code.
       01  ACODE PIC 999.
           88  CODE-FOR-MEDIUM-VALUE VALUE 29 THRU 41.
           88  CODE-FOR-SMALL-VALUE  VALUE 0 THRU 21.
           88  CODE-FOR-HIGH-VALUE   VALUE 50 THRU 71.
           88  CODE-FOR-LUXURY-VALUE VALUE 85 THRU 90.
           88  CODE-FOR-SUPERLUX     VALUE 95 THRU 100.
           88  CODE-FOR-ANALYSIS     VALUE 0 THRU 21
                                           50 THRU 71
                                           85 THRU 90
                                           95 THRU 100.
       01  ITEM PIC 999.
           88  ITEM-IS-MANAGERS-SPECIAL VALUE 500.



           IF ( ITEM-IS-MANAGERS-SPECIAL
              AND CODE-FOR-MEDIUM-VALUE )
           OR ( NOT ( ITEM-IS-MANAGERS-SPECIAL ) )
              AND ( CODE-FOR-SMALL-VALUE
                  OR  CODE-FOR-HIGH-VALUE
                  OR  CODE-FOR-LUXURY-VALUE
                  OR  CODE-FOR-SUPERLUX )
              stuff
           END-IF


Better with one 88 for all four tests in the second part:

           IF ( ITEM-IS-MANAGERS-SPECIAL
              AND CODE-FOR-MEDIUM-VALUE )
           OR ( CODE-FOR-ANALYSIS
              AND ( NOT ITEM-IS-MANAGERS-SPECIAL ) )
              stuff
           END-IF


Then why not a quiet "nested IF", (or an EVALUATE with two tests).

           IF ITEM-IS-MANAGERS-SPECIAL
              IF CODE-FOR-MEDIUM-VALUE
                  stuff
              END-IF
           ELSE
              IF CODE-FOR-ANALYSIS
                  same stuff as immediately above
              END-IF
           END-IF


Of course, I'd have all the "stuff" as a PERFORM (generally).