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
 

 

Testing for equality

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Fri Jul 10, 2015 9:19 pm    Post subject: Testing for equality
Reply with quote

Hi,

I thought of a logic today, that wish to both consult with moderators and share on this forum.

Many a times we need to check the values of multiple variables to the same value. Like for e.g. we have


Code:

IF      A  = SPACES 
   OR B  = SPACES
   OR C  = SPACES
   OR D  = SPACES
      DO SOMETHING
END-IF   



instead of checking each variable equal to SPACES, I thought of writing this code as

Code:

IF    SPACES  = ( A OR B OR C OR D)   
      DO SOMETHING
END-IF   


because basically this IF condition is check of true or false.

I compiled it, and it executed fine.


This sort of thing we have seen more in SQL QUERIES LIKE (1=1)
more . (Evaluate I think work similarly).

I wanted to confirm moderators if it is OK if we write our code like this or does it have any potential drawbacks?
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: Fri Jul 10, 2015 9:34 pm    Post subject:
Reply with quote

I don't see anything wrong with either way it's coded although the 1st way seems more straightforward and easier to read. I would prefer a 3rd way though -- an EVALUATE statement. Oftentimes, your shop standards dictate your coding conventions.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Fri Jul 10, 2015 9:37 pm    Post subject: Reply to: Testing for equality
Reply with quote

In your example, the letters "A" through "D" are consecutive in the EBCDIC collating sequence, so (for example) instead of testing for each letter, you could say NOT < 'A' and NOT > 'D', which would consider A-D as valid letters.

But, there are various other methods to do this, such as IF ALPHABETIC AND NOT = SPACE, which would verify only the upper-case letters of A-Z are acceptable.

HTH....
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Fri Jul 10, 2015 9:55 pm    Post subject: Reply to: Testing for equality
Reply with quote

Code:
IF DELIVERED-TODAY
OR DELIVERED-THIS-WEEK
OR DELIVERED-THIS-MONTH
OR DELIVERED-THIS-QUARTER
    DO THIS-EXACT-SOMETHING
END-IF


Where:

Code:
01 A PIC X.
    88 DELIVERED-TODAY VALUE SPACE.

01  B PIC X.
    88 DELIVERED-THIS-WEEK VALUE SPACE.

01  C PIC X.
    88 DELIVERED-THIS-MONTH VALUE SPACE.

01  D PIC X.
    88 DELIVERED-THIS-QUARTER VALUE SPACE.


I don't think EVALUATE helps here, as you'll get four places where DO THIS-EXACT-SOMETHING is referenced.

You example is certainly a way of rewriting your original code, and will operate as you expect.

You'll confuse people looking at/maintain your program, though, because they won't be familiar with that.

This will also give you your expected output (with your code):

Code:
IF    SPACES  =  A OR B OR C OR D   
      DO SOMETHING
END-IF


However, someone looking at that, to understand what it is doing, will have to look at the definitions of B and C and D, because one or more of those could be an 88.

I don't see how you think it is the same as (1=1), or what use it would be if it was.

People from a non-COBOL background are more likely to write like that, as a way of "protecting" the use of an "=" in an IF. However, since in COBOL an = is never "an assignment operator" there is absolutely no need to do that.

So, meaningful condition-names, meaningful data-names, and write for ease of understanding not for shortness of code, which will confuse. As Terry says, your site standards may have a lot of impact on how you should write it anyway.
Back to top
View user's profile Send private message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Fri Jul 10, 2015 10:31 pm    Post subject: Reply to: Testing for equality
Reply with quote

Thanks guys.

Bill,

Sure we could achieve the same feat using conditional names as you have explained and I myself use them more often than not as you have explained.

Moreover this was just and example where I had used variable names a A, B, C or D (in COBOL coding, we seldom use A, B , C or D as variable names either unless a specific need) and all I wanted to know if it works the way that I have explained without any drawbacks( I concur readability is one, but you know as coders our minds always look for all possible solutions same thing, icon_biggrin.gif and then picking the best one of the lot, given the time you have).

I agree the coding standards in an organization dictate how one writes his code. This has nothing to do with coding standards in my organization.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jul 11, 2015 3:59 am    Post subject: Reply to: Testing for equality
Reply with quote

If you consult the section on Relation Conditions in the Language Reference you'll see that all is fine with coding it in that way as far as COBOL is concerned:

Quote:
operand-1
The subject of the relation condition. Can be an identifier, literal, function-identifier, arithmetic expression, or index-name.


The reason it may be seen is due to the fear I mentioned earlier. In some languages "if a = 'a string'" is an assignment statement, usually unintended. "if 'a string' = a" won't compile, so putting any literal first gives some "protection" (but only with a string).

I don't see a benefit of doing it that way in COBOL, as people if people aren't used to that.
Back to top
View user's profile Send private message
himanshu_pant

New User


Joined: 08 Jul 2014
Posts: 14
Location: India

PostPosted: Mon Jul 13, 2015 2:46 pm    Post subject: Reply to: Testing for equality
Reply with quote

What!! Its already there???
I was thinking of making it my patent. icon_smile.gif icon_biggrin.gif

Thanks Bill. icon_smile.gif
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
No new posts Test for multiple equality prino CLIST & REXX 2 Tue Sep 15, 2015 12:47 am
This topic is locked: you cannot edit posts or make replies. Job Opening for Mainframe Testing Pro... gaurav.rajvanshi Mainframe Jobs 0 Thu Mar 19, 2015 2:23 pm
No new posts ADABAS to DB2 Data migration Testing Ashish.Srivastava.123 DB2 0 Fri Mar 06, 2015 2:13 am
No new posts Performance testing - Channels and Co... madhand Testing & Performance analysis 1 Tue Jan 20, 2015 10:14 am
No new posts Getting error while testing the Store... sivasaras DB2 1 Wed Sep 18, 2013 9:35 pm


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