Back to previous page...

The IF Statement

The general format is trivially simple:

IF A > B  MOVE 1 TO C.
or
IF A > B
   MOVE 1 TO C.
Please note that there is NO ENDIF word to terminate the IF statement. The IF statement is terminated with the "." (end of statement indicator).

If the IF statement has an ELSE part to execute,
IF A > B
   MOVE 1 TO C
ELSE
   MOVE 2 TO C.
Please note the position of the "."

You can put multiple statements within the body of IF and ELSE blocks :
IF A > B
   MOVE 1 TO C
   MOVE 1 TO D
   COMPUTE E = A + B + C
ELSE
   MOVE 2 TO C
   etc. etc.
   MOVE 0 TO D.
You can also nest IF statements :
IF A > B
   IF C > D
      MOVE 1 TO D
      COMPUTE E = A + B + C
   ELSE
      MOVE 2 TO C
ELSE
   MOVE 0 TO D.
Please note that, in nested IF statements, the "." goes to the end of the outermost IF.

COBOL has some extra IF constructs to help the programmer :
IF A IS NUMERIC MOVE 1 TO C.
or
IF B IS NOT ALPHABETIC MOVE 2 TO C.
You can type "more English like" IF statements :
IF B IS NOT NEGATIVE ....
IF C IS ZERO ....
IF E IS NOT ZERO ......
IF D IS POSITIVE ......
You can use compound tests in your IF statements :
IF B IS NOT NEGATIVE AND C = 1 AND D > 100  .....

IF ( C IS ZERO AND B = 100) OR K > 0  ....
Note the parenthesis used in the last sample IF statement because of the precedence of logical operators.

I have claimed many times that COBOL is an "English like" programming language; and here are few valid COBOL IF statements that support my claims :
IF SALARY > 30000000 AND < 50000000  .....

IF DEPT-CODE = "SALES" OR "ACCTG"

IF FINAL-VALUE = 1000  OR > 2000  ....
I believe that the above samples do not need any further explanation.

The NEXT SENTENCE CLAUSE

When you nest the IF statements, you will need a way to escape the nesting and pass the control out of the nestes IFs.

In such cases you should use the NEXT SENTENCE clause. I guess an example will clarify the usefulness of this clause :
IF A > B
   IF C > D
      ADD 1 TO X
   ELSE
      NEXT SENTENCE
ELSE
   IF M > N
      ADD 1 TO Y.
The NEXT SENTENCE clause skips all remaining parts of an IF statement nest and passes sequence control to the next statement which is expected after the IF termination (".").

The CONDITION NAME TESTS ( 88 Level Variables)

The 88 level variables are a means to write more readable code. The following example will make the picture clear :
     05  COLOR-CODE   PIC 9.
         88 RED          VALUE 1.
         88 GREEN        VALUE 2.
         88 BLUE         VALUE 3.
         88 WHITE        VALUE 0.
         88 BLACK        VALUE 9.

     ....
     ....
   
     IF BLUE MOVE BLUE-PAINT TO BRUSH. 
The last IF BLUE ... statement is equivalent to
     IF COLOR-CODE = 3 MOVE BLUE-PAINT TO BRUSH.
Some people believe that using 88 level entries make a program more readable and some other people believe that they complicate a program. Using 88 level entries or avoiding them is a matter of taste. If you like them use them; otherwise don't!.


Back to Structured Programming Elements...

Back to First Page...