6.4 IF..THEN..ELSE

Another fundamental part of programming logic is the ability to offer a choice of what to do that depends on the conditions asked of. (I'm not sure that makes any sense...). The format is:

IF {identifier-1} {condition} {identifier-2 or literal} ...
THEN {statements}
[ELSE {statements}]
END-IF

An example:

      IF X = Y THEN
         MOVE 1 TO Y-COUNTER
      ELSE
         MOVE 1 TO X-COUNTER
      END-IF


ELSE is used if an alternative statement is to be executed if the first condition is false. If there was only to be action if X = Y then the ELSE would be ommitted.

The END-IF terminates the IF statement. All that lies between IF and END-IF will depend on the conditions being tested.

Multiple conditions can be tested, i.e. IF (X = Y) AND (Y < 100) THEN ..

The types of conditions available are described in the following section..


1. Getting started 7. File handling
2. COBOL basics 8. Debugging COBOL
3. The Four Divisions 9. Useful links
4. Defining Data Part 1 10. Sample Code
5. Defining Data Part 2 11. Feedback
6. Commands and logic 12. Quick reference
   Awards    HOME
Copyright Timothy R P Brown 2000 - 2005: Web design in Glasgow, Scotland by 404i