Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Checking a condition using IF - Working in a wierd manner

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Checking a condition using IF - Working in a wierd manner
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

Site Director


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

PostPosted: Fri Feb 26, 2010 9:51 am    Post subject:
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    Post subject: Never, never use NOT with OR on the same field.
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

Site Director


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

PostPosted: Fri Feb 26, 2010 10:55 am    Post subject:
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    Post subject: Reply to: Checking a condition using IF - Working in a wierd
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    Post subject:
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    Post subject: Suggest you modify the newest code to NOT and AND
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: 1238
Location: Richfield, MN, USA

PostPosted: Sat Feb 27, 2010 9:06 am    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Sat Feb 27, 2010 8:25 pm    Post subject: Re: Suggest you modify the newest code to NOT and AND
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    Post subject: If you explain your logic intent in English,
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: 2150
Location: At my coffee table

PostPosted: Mon Mar 01, 2010 7:13 pm    Post subject:
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: 1218
Location: Israel

PostPosted: Mon Mar 01, 2010 7:20 pm    Post subject:
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    Post subject:
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: 1238
Location: Richfield, MN, USA

PostPosted: Mon Mar 01, 2010 8:33 pm    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. How to pass the previous month date i... Suganya87 DFSORT/ICETOOL 5 Mon Oct 31, 2016 4:13 pm
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Syncsort - NULL in Integer field chec... nartcr SYNCSORT 4 Thu Oct 06, 2016 6:47 am
No new posts Reasonable way of checking wildcards ... sergeyken CLIST & REXX 5 Wed Sep 07, 2016 8:43 pm
No new posts SCRNAME not working on my mainframe? mrsidhu12 TSO/ISPF 7 Thu Sep 01, 2016 2:17 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us