IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Checking a condition using IF - Working in a wierd manner


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
TAPAS PATEL

New User


Joined: 03 Apr 2009
Posts: 47
Location: CHENNAI

PostPosted: Fri Feb 26, 2010 9:46 am
Reply with quote

Hi All,
I was working on a online program. In the course of the program I came across a logic where I had to check for a variable which is Alphanumeric. The check to look if the value of variable is not equal to a particular string, if yes then the logic works in a specific manner. To illustrate my understanding I have simulated the scenario using a simple cobol program, please find the code and the o/p below.

Program:

Code:

WORKING-STORAGE SECTION.                         
                                                 
                                                 
01  WS-A                    PIC  X(06).           
01  WS-C                    PIC  X(06).           
     
                                                 
LINKAGE SECTION.                                 
                                                 
                                                 
PROCEDURE DIVISION.                               
 EJECT                                           
MAIN-CONTROL-PARAGRAPH.                           
                                                 
    MOVE 'CHECK1'  TO  WS-A.                     
    MOVE 'CHECK2'  TO  WS-C.                     
    DISPLAY 'BEFORE IF CHECK WS-A: ' WS-A.       
    IF WS-A   NOT  =  'CHECK1'                   
                         OR  'CHECK2'                   
       DISPLAY 'LIFE AFTER IF!!!'
       DISPLAY 'WS-A: ' WS-A                     
    END-IF.                                       



Output:

Code:

 SDSF OUTPUT DISPLAY TESTCOB  JOB07913  DSID   106 LINE 0   
 COMMAND INPUT ===>                                         
********************************* TOP OF DATA **************
BEFORE IF CHECK WS-A: CHECK1                               
LIFE AFTER IF
WS-A: CHECK1                                               
******************************** BOTTOM OF DATA ************


I tried the same with the brackets and the result is same. I am puzzled at this, this small thing in a 8000 lines code had taken my complete night to figure out. Please let me know the reason for this wierd behaviour.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Feb 26, 2010 9:51 am
Reply with quote

Hello,

Never, never use NOT with OR on the same field. It cannot possibly work.

CHECK1 is not CHECK2. CHECK2 is not CHECK1. So every value meets the NOT.
Back to top
View user's profile Send private message
TAPAS PATEL

New User


Joined: 03 Apr 2009
Posts: 47
Location: CHENNAI

PostPosted: Fri Feb 26, 2010 10:34 am
Reply with quote

Hello Dick,
Thanks for the reply. In this scenario how will I check the condition. I have changed the code as below but still no luck..

Program:

Code:

PROCEDURE DIVISION.                           
 EJECT                                         
MAIN-CONTROL-PARAGRAPH.                       
                                               
    MOVE 'CHECK1'  TO  WS-A.                   
    MOVE 'CHECK2'  TO  WS-C.                   
    DISPLAY 'BEFORE IF CHECK WS-A: ' WS-A.     
    IF (WS-A   NOT  =  'CHECK1') OR           
       (WS-A   NOT  =  'CHECK2')               
       DISPLAY 'LIFE AFTER IF!!'               
       DISPLAY 'WS-A: ' WS-A                   
    END-IF.                                   


I am looking at using 88 level variable for this. I think this will work, but then is there any other way?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Feb 26, 2010 10:55 am
Reply with quote

Hello,

Once again, you have code NOT and OR for the same field. . . As before, CHECK1 is not = CHECK2. CHECK2 is not = CHECK1. So every value meets the NOT.

Suggest you modify the newest code to NOT and AND. . . And learn why this has not worked rather than swith to some other solution. . .
Back to top
View user's profile Send private message
bijal.awhad

New User


Joined: 19 Mar 2008
Posts: 51
Location: Pune

PostPosted: Fri Feb 26, 2010 1:20 pm
Reply with quote

Hi Tapas,

I faced similar issue for one online program & when i used NOT and AND it worked.

I took the help of logic gate truth table to understand it.

Truth table of NAND gate
Code:

INPUT   OUTPUT
A B     A NAND B
0 0        1
0 1        1
1 0        1
1 1        0


Truth table of NOR gate
Code:

INPUT   OUTPUT
A B    A NOR B
0 0       1
0 1       0
1 0       0
1 1       0

Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Fri Feb 26, 2010 3:53 pm
Reply with quote

You misinterpret the above truth tables in the original problem.

You have 2 independent conditions in the truth tables. In your IF evaluation, what does it take to make both conditions false, which is the only instance where the whole IF condition is false?

If WS-A not = 'CHECK1' is false, then WS-A has the value 'CHECK1'.
But then the second condition WS-A not = 'CHECK2' will be true, because WS-A has the value 'CHECK1'! You cannot find a value that will cause both conditions to be false!
Back to top
View user's profile Send private message
TAPAS PATEL

New User


Joined: 03 Apr 2009
Posts: 47
Location: CHENNAI

PostPosted: Fri Feb 26, 2010 3:57 pm
Reply with quote

Hello All,
Thanks for the info..I have tried with AND and it looks to be working now. Lessons learnt - "NOT and OR on same variable are not advisable".
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat Feb 27, 2010 9:06 am
Reply with quote

Most shops discourage the use of NOT whenever possible for the very reason pointed out -- it is easily misinterpreted. If you explain your logic intent in English, we can suggest some of the more acceptable ways of wording your IF or EVALUATE statement.
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Sat Feb 27, 2010 8:25 pm
Reply with quote

TAPAS PATEL wrote:
I have tried with AND and it looks to be working now. Lessons learnt - "NOT and OR on same variable are not advisable".
Think of it as:
IF WS-A NOT = 'CHECK1' AND (at the same time) WS-A NOT = 'CHECK2'
Back to top
View user's profile Send private message
TAPAS PATEL

New User


Joined: 03 Apr 2009
Posts: 47
Location: CHENNAI

PostPosted: Mon Mar 01, 2010 3:40 pm
Reply with quote

Hi Terry,
I have a requirement where there are 10 types of records which I can encounter, lets say REC1 to REC10. I process these records based on some business logic. In the course of processing there is a need for me to ignore records of type REC3 and REC7 and process others. I was initially trying to get this done by using NOT and OR on REC_TYPE. But now I have defined 88 level variable for REC TYPE and checking with that.

Yes - 'IF WS-A NOT = 'CHECK1' AND (at the same time) WS-A NOT = 'CHECK2'' is also one of the probable solutions as was pointed out by Dick earlier.
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Mon Mar 01, 2010 7:13 pm
Reply with quote

Have you considered the EVALUATE statement?
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Mon Mar 01, 2010 7:20 pm
Reply with quote

Sometimes I like to use both NOT and OR.
The parenthesis make it easy to understand (IMHO):
Code:
IF NOT (WS-A = 'CHECK1' OR 'CHECK2') THEN
    DISPLAY 'WS-A IS NOT ONE OF THE LISTED VALUES'
END-IF
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Mon Mar 01, 2010 7:35 pm
Reply with quote

Marso wrote:
Sometimes I like to use both NOT and OR.
The parenthesis make it easy to understand (IMHO):
Code:
IF NOT (WS-A = 'CHECK1' OR 'CHECK2') THEN
    DISPLAY 'WS-A IS NOT ONE OF THE LISTED VALUES'
END-IF

You're begging for trouble...

If somebody else eventually will have to maintain your code, they will have have to put extra effort into understanding your logic when a prefixed negation is involved. It would be more straightforward to code:
Code:
IF WS-A = 'CHECK1' OR 'CHECK2' THEN
     NEXT SENTENCE
  ELSE
     DISPLAY 'WS-A IS NOT ONE OF THE LISTED VALUES'
END-IF
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Mon Mar 01, 2010 8:33 pm
Reply with quote

As CICS-Guy mentioned and I alluded to, EVALUATE might be a better choice over an IF statement. See the examples in the Language Reference Manual.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Fri Mar 05, 2010 7:14 pm
Reply with quote

Terry Heinze wrote:
As CICS-Guy mentioned and I alluded to, EVALUATE might be a better choice over an IF statement. See the examples in the Language Reference Manual.

Using EVALUATE does not relieve the developer of being able to understand and construct boolean logic.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts PD not working for unsigned packed JO... DFSORT/ICETOOL 5
No new posts Def PD not working for unsigned packe... JCL & VSAM 3
No new posts ICETOOL with JOINKEY for Big record l... DFSORT/ICETOOL 12
No new posts How to give complex condition in JCL . CLIST & REXX 30
No new posts REXX - Dataset checking in a do forev... CLIST & REXX 6
Search our Forums:

Back to Top