Regarding the Logic



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Regarding the Logic

Postby vinu78 » Fri Feb 01, 2013 8:46 pm

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
vinu78
 
Posts: 29
Joined: Sat Sep 27, 2008 4:47 am
Has thanked: 0 time
Been thanked: 0 time

Re: Regarding the Logic

 

Re: Regarding the Logic

Postby enrico-sorichetti » Fri Feb 01, 2013 9:00 pm

add an ( ITEM <not equal> 500 ) to the first two conditions
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2643
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Regarding the Logic

Postby Robert Sample » Fri Feb 01, 2013 9:25 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: Regarding the Logic

Postby vinu78 » Fri Feb 01, 2013 9:26 pm

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
vinu78
 
Posts: 29
Joined: Sat Sep 27, 2008 4:47 am
Has thanked: 0 time
Been thanked: 0 time

Re: Regarding the Logic

Postby Robert Sample » Fri Feb 01, 2013 9:49 pm

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: Regarding the Logic

Postby BillyBoyo » Sat Feb 02, 2013 9:37 pm

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).
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post