View previous topic :: View next topic |
Author |
Message |
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Thu Jul 05, 2012 10:57 am Post subject: COBOL Combined Condition |
|
|
Came across the following piece of code:
Code: | IF WS-INDICATOR NOT = ('Y' AND 'P') |
Question:
Does the 'AND' in the statement get converted to an 'OR' ? If not, when will this statement be true ? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Thu Jul 05, 2012 11:16 am Post subject: |
|
|
Vini,
The IF statement is translated
Code: |
IF WS-INDICATOR NOT = 'P' OR
WS-INDICATOR NOT = 'Y'
|
Someone thought of smarter way to avoid coding 2 lines for an IF condition |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Thu Jul 05, 2012 1:34 pm Post subject: |
|
|
When you have a doubt like this, you can look at the code generated by the compiler to see how the source code is translated. _________________ All the best,
di |
|
Back to top |
|
|
jim haire Beginner
Joined: 30 Dec 2002 Posts: 140 Topics: 40
|
Posted: Thu Jul 05, 2012 4:03 pm Post subject: |
|
|
I think the statement is translated as:
Code: |
IF WS-INDICATOR NOT = 'P' AND
WS-INDICATOR NOT = 'Y'
|
It doesn't translate to using OR. The statement:
Code: |
IF WS-INDICATOR NOT = 'P' OR
WS-INDICATOR NOT = 'Y'
|
is always true. Things should never be coded this way.
If WS-INDICATOR was equal to "Y", the first clause of the IF statement would be true. If WS-INDICATOR was equal to "P", the second clause of the IF statement would be true. If WS-INDICATOR was any other value, both clauses of the IF statement would be true. |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Thu Jul 05, 2012 4:30 pm Post subject: |
|
|
Kolusu,
I thought I had heard something similar before but wanted to double check.
Ever wonder why this translation occurs ? Isn't COBOL supposed to be very english-like for enhanced readability ? This seems against that grain...nope ?
Papadi,
I was looking at compiler listing itself. Is the translated code embedded within the cobol or in a seperate section in the listing? I dont see any translation right above or below this stmt.
Thanks, |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Thu Jul 05, 2012 8:59 pm Post subject: |
|
|
Does you compile specify LIST or NOLIST? If NOLIST is specified, the translated code is not listed.
If NOLIST is specified, Add a statement as the first line of your source. Then remove this as soon as you have run your test. It adds a lot of print that is usually not needed.
Quote: |
Isn't COBOL supposed to be very english-like for enhanced readability ? This seems against that grain...nope ? | Well, COBOL is a Business Oriented Language. It is much easier to read than some of the more cryptic things around. As with any discipline there is the entry level and then the more advanced. I still believe that code should be easily read/understood by people with minimal experience (they might not be able to write the code, but it should be written so that it is easily understood and maintainable. There is no business reason to write "cute" or "clever" code. _________________ All the best,
di |
|
Back to top |
|
|
NorthernDancer Beginner
Joined: 01 May 2007 Posts: 44 Topics: 22 Location: DOWNTOWN BUFFALO, NY
|
Posted: Fri Jul 06, 2012 9:31 am Post subject: |
|
|
This combination is ALWAYS confusing. But somehow IBM comes out with NOT AND NOT = OR; NOT OR NOT = AND. I try to use the +VE tests when possible. Can't always do that though. If it's a code value being tested and the values are pretty rigid, then can set up W.S. Element with an 88 condition name list and omit the one value you're testing for with the NOT. Move the file field or input filed to that, and then test that. It then becomes a +VE test. |
|
Back to top |
|
|
Anuj Dhawan Intermediate
Joined: 19 Jul 2007 Posts: 298 Topics: 7 Location: Mumbai,India
|
Posted: Fri Jul 06, 2012 9:47 am Post subject: |
|
|
Yep, this is confusing, This
Code: | MOVE 'P' TO WS-INDICATOR
IF WS-INDICATOR NOT = ('Y' AND 'P')
DISPLAY '<TRUE>'
DISPLAY '<WS-INDICATOR>' WS-INDICATOR
ELSE
DISPLAY '<FALSE>'
DISPLAY '<WS-INDICATOR>' WS-INDICATOR
END-IF |
Produces:
Code: | <FALSE>
<WS-INDICATOR>P |
And this
Code: | IF WS-INDICATOR NOT = 'P' OR
WS-INDICATOR NOT = 'Y'
DISPLAY '<TRUE>'
DISPLAY '<WS-IND>' WS-INDICATOR
ELSE
DISPLAY '<FALSE>'
DISPLAY '<WS-IND>' WS-INDICATOR
END-IF |
Produces:
while this
Code: | IF WS-INDICATOR NOT = 'P' AND
WS-INDICATOR NOT = 'Y'
DISPLAY '<TRUE>'
DISPLAY '<WS-IND>' WS-INDICATOR
ELSE
DISPLAY '<FALSE>'
DISPLAY '<WS-IND>' WS-INDICATOR
END-IF |
Produces:
And if WS-INDICATOR is SET to A -- all the IFs are TRUE. _________________ Regards,
Anuj |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Fri Jul 06, 2012 10:23 am Post subject: |
|
|
Simple rulle-of-thumb is to NEVER use NOT with OR on the same field in an IF statement.
And this improper code can be found in many programs that have been "working" for years. . . _________________ All the best,
di |
|
Back to top |
|
|
William Collins Supermod
Joined: 03 Jun 2012 Posts: 437 Topics: 0
|
Posted: Fri Jul 06, 2012 2:46 pm Post subject: |
|
|
Code: | IF ( WS-INDICATOR NOT EQUAL TO 'Y' )
AND ( WS-INDICATOR NOT EQUAL TO 'P' )
...
END-IF |
Much better still, just an 88. Descriptive and easier to follow/maintain.
I always write out the conditions lengthily and in parentheses where multiple. For the human reader, so they never have to think "what order does the compiler do that in." If everything in parenthesis, the compiler will do what you say. |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Fri Jul 06, 2012 4:44 pm Post subject: |
|
|
Papadi .... After understanding how this works with help of Anujs findings ... I must say you are absolutely right about avoiding OR on the same field in an IF.
The original statement in question (one with AND) was probably written by the coder to avoid the problem with OR on same field.
So even though at first look it seems confusing ..it is actually avoiding the potential problem which an OR would otherwise create. |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Fri Jul 06, 2012 4:45 pm Post subject: |
|
|
Ooops I meant NOT with OR in the same field ... and not just an OR alone in an IF... |
|
Back to top |
|
|
William Collins Supermod
Joined: 03 Jun 2012 Posts: 437 Topics: 0
|
Posted: Fri Jul 06, 2012 7:27 pm Post subject: |
|
|
It was not coded that way to avoid anything. It was coded that way to be lazy/"clever".
If you put an 88 on the field and code the IF, see how easy it would have been and consider how much time you would have saved, let alone ours. You'd never have had to post.
Get a change authorised for it, and get it changed to save the next person a similar problem - and that might be you in a few months time... |
|
Back to top |
|
|
misi01 Advanced
Joined: 02 Dec 2002 Posts: 629 Topics: 176 Location: Stockholm, Sweden
|
Posted: Tue Dec 10, 2013 8:59 am Post subject: |
|
|
papadi wrote: | When you have a doubt like this, you can look at the code generated by the compiler to see how the source code is translated. |
....or you could rewrite it and fire the person who wrote it _________________ Michael |
|
Back to top |
|
|
Dibakar Advanced
Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Thu Jan 02, 2014 3:23 pm Post subject: |
|
|
Code: | IF WS-INDICATOR NOT = ('Y' AND 'P') |
In my interaction with business analysts, they don't get confused with code like this, but the programmers always are. I think it is okay since it is supposed to be business orinded, not coder oriented. _________________ Regards,
Diba |
|
Back to top |
|
|
|
|