View previous topic :: View next topic
|
Author |
Message |
TAPAS PATEL
New User
Joined: 03 Apr 2009 Posts: 47 Location: CHENNAI
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
TAPAS PATEL
New User
Joined: 03 Apr 2009 Posts: 47 Location: CHENNAI
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
bijal.awhad
New User
Joined: 19 Mar 2008 Posts: 51 Location: Pune
|
|
|
|
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 |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
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 |
|
|
TAPAS PATEL
New User
Joined: 03 Apr 2009 Posts: 47 Location: CHENNAI
|
|
|
|
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 |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
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 |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
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 |
|
|
TAPAS PATEL
New User
Joined: 03 Apr 2009 Posts: 47 Location: CHENNAI
|
|
|
|
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 |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
Back to top |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
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 |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
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 |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
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 |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
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 |
|
|
|