COBOL TIPS #31
by
Shawn M. Gordon
President S.M.Gordon & Associates

Once again we are back with our continuing ordeal, comparing the C and COBOL languages, and trying to find as many equivalents as possible.  Last month we covered simple and complex variable declarations and initializations.  This month we are going to cover operators, and looping constructs, so read on.

OPERATORS

Let's first go through the difference between = and == in C.  In COBOL there a re several ways to get data into a variable.  A common way is the MOVE verb, the equivalent in C is =.  This is confusing because if you set up a logical test and use = it will always evaluate to true because the value on the right hand side of the equal sign will be assigned to the variable on the left side. You need to use == if you want to compare values.  Here is a simple mistake to make that can cause all sorts of problems.  If in C I were to say

   if(my_int = 5)

what would happen is that the variable 'my_int' would be assigned the value 5 and since the assignment went successfully then it would return a non-zero value which would indicate true.  This would make the 'if' statement evaluate as true. So this 'if' statement would always be true.  Simple mistake, major repercussions, and the compiler won't complain about it because it's a valid statement. Make sure you learn the distinction between = and == early and never forget it.

Our next boolean operator is 'AND'.  COBOL makes it very easy to compare multiple values in an IF statement;  
    IF (VAR1 = VAL1) AND (VAR2 = VAL2 OR VAL3) THEN

The C representation for 'AND' is '&&', 'OR' is '||', and 'NOT EQUAL' is identified by '!='.

None of these are particularly difficult to learn, but they are less than intuitive when first learning the language.

Being creative you could make use of the #define to make the operator's anything you want, for example;

#define AND &&
#define OR ||
#define NOT !=
#define EQUALS ==

Now this may tick off the other C programmers, but if it helps you to make your code more readable and easier to maintain, who's to say it's wrong?  

ASSIGNMENT OPERATORS

Since I already talked about how the = sign in C is equivalent to the COBOL  MOVE verb, I won't talk about it again.  However another option to using the = sign in C is to use the string function 'strcpy'.  This performs a 'string copy' into a variable.  This is a good way to initialize a character array and make sure that a null is properly placed (strcpy appends the null to the end of the  string copied in), especially if you want to append data to the string later. The verb in C to append strings is 'strcat' for 'string concatenation'. In COBOL we have the 'STRING' verb, which gives you much finer control over how variables are concatenated.  All the 'strcat' does is copy one character until it hits a null into another character array, starting at the terminating null. You can see now why having a null in the right place is so important.  You can also use the memory function 'memcpy' even you want to copy raw byte memory locations without having to worry about the NULL, but we aren't going to get into the memory functions much here.

There are a couple of really cool increment operator shortcuts in C that I absolutely love.  In COBOL if you want to increment a variable in a loop for instance you can do it a couple of ways;

 ADD 1 TO KOUNT
 COMPUTE KOUNT = KOUNT + 1
 ADD 1 TO KOUNT GIVING KOUNT
 PERFORM VARYING KOUNT FROM 1 BY 1 UNTIL KOUNT >= MAX

In C you could say the following;

 kount = kount + 1
  kount += 1
 kount ++
 for (kount = 1; kount >= max; kount++)

The first example should be obvious, in the second example the += is a shortcut of the first example, it means to include the variable on the left side of the = sign in the computation on the right side.  The third example means increment the value by 1.  Our first two examples could have had any value in the addition, but the third one simply increments by one.  The last example is identical to the last COBOL example.  What is interesting is the last parameter 'kount++', by putting the ++ on the right side of the variable we are saying to increment the variable after the test is made as to weather it is greater than or equal to 'max'. If the ++ is put to the left side, as in '++kount', it means to increment BEFORE the test is made.  This is the same as the PERFORM directive 'WITH TEST BEFORE' or 'WITH TEST AFTER'. The - sign can be used to decrement in the exact same fashion, i.e.,= or --.  

LOGICAL CONSTRUCTS

Hopefully everyone here is familiar with the COBOL PERFORM statement. It has more variations than I am willing to get into, but it is the only looping construct it has, unless you count using GO TO.  C offers several different looping controls, there is;

   while    do..while    for

Now COBOL nicely lumps the functionality of both the 'while' and 'for' loops into the PERFORM.  The 'do..while' loop however is not explicitly the same, you can simulate it by controlling your variables correctly. In essence, the difference  between 'while' and 'do..while' is that in the 'do..while' loop, it will 'do' the loop at least once since the test isn't until AFTER the loop has been executed once. In the 'while' loop your test may not be valid the first time you do it so you may never actually go through the loop.

int i = 21;

while(i++ < 20) {
 printf("%d\n",i);
}
 /* the above line will display nothing since i is already greater than 20 */

do
 {
 printf("%d\n",i);
 } while(i++ < 20);

/* the above line will display 21 */

Let's talk about the 'if' statement, here is an example of how confusing a string comparison operation can be. In COBOL the following statement is very straight forward

     IF STRING1 IS EQUAL TO STRING2

You cannot compare strings that way in C.  There is a function in the 'string.h'  header file that will compare two strings, however to get the same result it would have to be worded as follows;

     if (!strncmp(string1,string2)) {

Let me explain, first off 'strncmp' is a string compare function, if string1 is  less than string2 then a value less than zero will be returned (sort of like using Condition Code in COBOL).  If string1 is equal to string2 then a value of zero is returned.  If string1 is greater than string2 then a value greater than zero is returned.  The problem is that if you use strncmp in an IF statement and the strings are equal then zero is returned, zero indicates that the IF statement is false, that is why we prefaced the 'strncmp' with '!' which means not.  This has the net result of returning a non-zero value, which is TRUE for the IF statement, if string1 and string2 are equal.  This also further illustrates how logical expressions can be embedded in the 'if' statement.

That's it for this month, next month we will cover I/O from the two languages, after all you do want to get input from the user, and display output, right?