COBOL If and End If

If and End-If Example Number 1

004320     IF X > 10
004400         MOVE 5 TO Y
004430     END-IF.

If and End-If Example Number 2

004320     IF X > 10
004330         IF Y > 10
004340             MOVE 5 TO Z
004350         ELSE
004360             MOVE 3 TO Z   
004370         END-IF
004380     ELSE
004390         MOVE 1 TO Z
004400     END-IF.
Comments Comments are left by visitors to and may or may not be accurate.
Comment by rslitman on 2013-05-22 Rate this Comment

I'm glad to see indentations of four spaces at each level.

When I do an ELSE followed by another IF (rarer these days thanks to EVALUATE), I still keep the ELSE on a line by itself. The new IF starts on a new line, indented four spaces, of course.

Some programming languages have a construct called ELSEIF. This totally goes against my style! Some of the earliest PC-based compilers for languages such as BASIC and PASCAL would reformat my typing. If I did an ELSE on a line by itself, with the IF starting, indented, on the next line, it would reformat it into one of those horrendous ELSEIF's!

When a language (such as IBM mainframe COBOL) doesn't require a THEN in an ELSE construct, I omit it. Some languages/compilers I've worked with require THEN. I tend to put it on a line by itself, similar to how I stick my ELSE's.

Sign in to comment on COBOL If and End If.