MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

COBOL Combined Condition
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Thu Jul 05, 2012 10:57 am    Post subject: COBOL Combined Condition Reply with quote

Came across the following piece of code: Confused
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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Jul 05, 2012 11:16 am    Post subject: Reply with quote

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 Very Happy
Back to top
View user's profile Send private message Send e-mail Visit poster's website
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Thu Jul 05, 2012 1:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Thu Jul 05, 2012 4:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Thu Jul 05, 2012 4:30 pm    Post subject: Reply with quote

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, Surprised
Back to top
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Thu Jul 05, 2012 8:59 pm    Post subject: Reply with quote

Does you compile specify LIST or NOLIST? If NOLIST is specified, the translated code is not listed.

If NOLIST is specified, Add a
Code:
 
       CBL LIST
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
View user's profile Send private message
NorthernDancer
Beginner


Joined: 01 May 2007
Posts: 44
Topics: 22
Location: DOWNTOWN BUFFALO, NY

PostPosted: Fri Jul 06, 2012 9:31 am    Post subject: Reply with quote

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
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Fri Jul 06, 2012 9:47 am    Post subject: Reply with quote

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:
Code:
<TRUE>   
<WS-IND>P

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:
Code:
<FALSE> 
<WS-IND>P


And if WS-INDICATOR is SET to A -- all the IFs are TRUE.
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Fri Jul 06, 2012 10:23 am    Post subject: Reply with quote

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
View user's profile Send private message
William Collins
Supermod


Joined: 03 Jun 2012
Posts: 437
Topics: 0

PostPosted: Fri Jul 06, 2012 2:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Fri Jul 06, 2012 4:44 pm    Post subject: Reply with quote

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
View user's profile Send private message
vini
Intermediate


Joined: 12 Jan 2004
Posts: 240
Topics: 48
Location: Maryland

PostPosted: Fri Jul 06, 2012 4:45 pm    Post subject: Reply with quote

Ooops I meant NOT with OR in the same field ... and not just an OR alone in an IF...
Back to top
View user's profile Send private message
William Collins
Supermod


Joined: 03 Jun 2012
Posts: 437
Topics: 0

PostPosted: Fri Jul 06, 2012 7:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
misi01
Advanced


Joined: 02 Dec 2002
Posts: 629
Topics: 176
Location: Stockholm, Sweden

PostPosted: Tue Dec 10, 2013 8:59 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Thu Jan 02, 2014 3:23 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group