COBOL
The original of this I wrote some 30 years ago, so if the prize was for the oldest living, if brought-back-to-life, example of such a task... I re-wrote it about three years ago. After I had posted parts of it, parts appeared on some French web-site. This is the original and full working program, which I have posted some time last year on a LinkedIn group.
It still "works" today, and would have "worked" with the first COBOL compilers. GNU Cobol is readily available (SourgeForge) for anyone who would like to test the program...
The reason for writing it is mentioned in comments. Explanation follows the code. Enjoy :-)
ID DIVISION.
PROGRAM-ID. DEEPDOO.
*REMARKS. THE PURPOSE OF THIS PROGRAM IS TO SHOW THAT, FAR
* FROM "THAT CAN'T BE THE PROBLEM, COBOL DOESN'T DO
* THAT", IT IS, AND IT DOES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A1-FG PIC X VALUE SPACE.
01 B1-FG PIC X VALUE SPACE.
01 C1-FG PIC X VALUE SPACE.
PROCEDURE DIVISION.
OBLIGATORY-SECTION SECTION.
OB-PARA.
PERFORM A
IF A1-FG EQUAL TO "Y"
DISPLAY "DO"
GO TO G6
END-IF
.
G1.
PERFORM B
IF B1-FG EQUAL TO "Y"
DISPLAY "DOO"
GO TO G5
END-IF
.
G2.
PERFORM C
IF C1-FG EQUAL TO "Y"
DISPLAY "IN DEEP"
GO TO G4
END-IF
.
G3.
GO TO C5
.
G4.
GO TO B5
.
G5.
GO TO A5
.
G6.
MOVE +11 TO RETURN-CODE
GOBACK
.
A SECTION.
A0-PARA.
GO TO G1
.
A5.
MOVE "Y" TO A1-FG
.
A9.
EXIT.
B SECTION.
B0-PARA.
GO TO G2
.
B5.
MOVE "Y" TO B1-FG
.
B9.
EXIT.
0A SECTION.
0-PARA.
DISPLAY "I WAS GOING ALONG QUIETLY AND NOW I'M"
.
C SECTION.
C0-PARA.
IF C1-FG NOT EQUAL TO "Y"
GO TO G3
ELSE
GO TO C9
END-IF
.
C5.
MOVE "Y" TO C1-FG
GO TO 0-PARA
.
C9.
EXIT.
Z-OBFUSCATION SECTION.
Z-OB.
DISPLAY "IT DOESN'T GET HERE, NOT IN A MILLION YEARS"
STOP RUN.
There, wasn't so bad, was it?
Output is:
I WAS GOING ALONG QUIETLY AND NOW I'M
IN DEEP
DOO
DO
Which is just a different way of spelling
Line4
Line3
Line2
Line1
When COBOL was designed, it was done such that a label (a paragraph or a SECTION) could be the target of a PERFORM or a GO TO or just "fallen into" sequentially, and even, though not explicitly stated, any combination for the same label.
The problem arises when the same label is already under the scope of a PERFORM, a GO TO (accidentally) comes out of the range of the PERFORM (leaving it still "active") and control later arrives at the same label. If progress is then smoothly to the end of the scope of the original PERFORM, then control will be returned to the statement after the original PERFORM, no matter what else has happened since (as long as an infinite loop has not been created).
Very, very, bad practice to knowingly make use of that. Very. Can't stress that enough. Very, very, bad.
However, carelessness in whirling chunks of code about in an editor can cause this unintentionally. It can also be deliberately coded, without realising the consequences (example on SO recently, but without causing this type of problem).
However, when it does happen, the consequences evident seem so unlikely that people do not believe it. Parts of a program appear to, and actually do, run out of their expected sequence.
Of course, the "expected sequence" is just wrong, not the actual sequence, until you know what to expect, and then everything is OK (except the program doesn't work).
After two people came to me within a week with this type of problem, and didn't believe what I told them (although that bit of their code worked after my pointing out the errant GO TOs) I wrote the program to show these and any future Doubting Thomases.
For fun I compiled it using GNU COBOL with the -debug switch, which lists each verb and label as they are encountered. Here is the output:
Program-Id: DEEPDOO Statement: PERFORM Line: 15
Program-Id: DEEPDOO Section: A Line: 48
Program-Id: DEEPDOO Paragraph: A0-PARA Line: 49
Program-Id: DEEPDOO Statement: GO TO Line: 50
Program-Id: DEEPDOO Paragraph: G1 Line: 21
Program-Id: DEEPDOO Statement: PERFORM Line: 22
Program-Id: DEEPDOO Section: B Line: 57
Program-Id: DEEPDOO Paragraph: B0-PARA Line: 58
Program-Id: DEEPDOO Statement: GO TO Line: 59
Program-Id: DEEPDOO Paragraph: G2 Line: 28
Program-Id: DEEPDOO Statement: PERFORM Line: 29
Program-Id: DEEPDOO Section: C Line: 70
Program-Id: DEEPDOO Paragraph: C0-PARA Line: 71
Program-Id: DEEPDOO Statement: IF Line: 72
Program-Id: DEEPDOO Statement: GO TO Line: 73
Program-Id: DEEPDOO Paragraph: G3 Line: 35
Program-Id: DEEPDOO Statement: GO TO Line: 36
Program-Id: DEEPDOO Paragraph: C5 Line: 78
Program-Id: DEEPDOO Statement: MOVE Line: 79
Program-Id: DEEPDOO Statement: GO TO Line: 80
Program-Id: DEEPDOO Paragraph: 0-PARA Line: 67
Program-Id: DEEPDOO Statement: DISPLAY Line: 68
I WAS GOING ALONG QUIETLY AND NOW I'M
Program-Id: DEEPDOO Section: C Line: 70
Program-Id: DEEPDOO Paragraph: C0-PARA Line: 71
Program-Id: DEEPDOO Statement: IF Line: 72
Program-Id: DEEPDOO Statement: GO TO Line: 75
Program-Id: DEEPDOO Paragraph: C9 Line: 82
Program-Id: DEEPDOO Statement: EXIT Line: 83
Program-Id: DEEPDOO Statement: IF Line: 30
Program-Id: DEEPDOO Statement: DISPLAY Line: 31
IN DEEP
Program-Id: DEEPDOO Statement: GO TO Line: 32
Program-Id: DEEPDOO Paragraph: G4 Line: 38
Program-Id: DEEPDOO Statement: GO TO Line: 39
Program-Id: DEEPDOO Paragraph: B5 Line: 61
Program-Id: DEEPDOO Statement: MOVE Line: 62
Program-Id: DEEPDOO Paragraph: B9 Line: 64
Program-Id: DEEPDOO Statement: EXIT Line: 65
Program-Id: DEEPDOO Statement: IF Line: 23
Program-Id: DEEPDOO Statement: DISPLAY Line: 24
DOO
Program-Id: DEEPDOO Statement: GO TO Line: 25
Program-Id: DEEPDOO Paragraph: G5 Line: 41
Program-Id: DEEPDOO Statement: GO TO Line: 42
Program-Id: DEEPDOO Paragraph: A5 Line: 52
Program-Id: DEEPDOO Statement: MOVE Line: 53
Program-Id: DEEPDOO Paragraph: A9 Line: 55
Program-Id: DEEPDOO Statement: EXIT Line: 56
Program-Id: DEEPDOO Statement: IF Line: 16
Program-Id: DEEPDOO Statement: DISPLAY Line: 17
DO
Program-Id: DEEPDOO Statement: GO TO Line: 18
Program-Id: DEEPDOO Paragraph: G6 Line: 44
Program-Id: DEEPDOO Statement: MOVE Line: 45
Program-Id: DEEPDOO Statement: GOBACK Line: 46
Program-Id: DEEPDOO Exit: DEEPDOO
Line: 80 is what causes the unwinding of the PERFORMs to start, as control will "fall through" from the target of that GO TO into the following SECTION.
This mimics the situation where a SECTION in the program has been copied, modified, but the "GO TO the named paragraph which happens to be the end of the PERFORM range has not been changed - so it GO TOs the end of the wrong SECTION, and "falls through" into the next SECTION and continues falling until it stumbles across a PERFORM-range which is still active, or it gets into a Big Fat Loop or the end of the program is reached.
There are COBOL programmers who feel it is OK to use GO TO
to get to the last paragraph of a PERFORM-range (I'm not one). If doing this, and using SECTIONs, it is best to name the final paragraph in each SECTION identically. The compiler will then automatically "qualify" that paragraph-reference in the GO TO to being the one in the current SECTION.
15Does Arabic count? : ) – None – 2014-02-13T08:17:28.277
If you are able to meet the specs, of course :P – Vereos – 2014-02-13T10:34:19.213
Wanted to quickly clarify one rule... When you say "Every like can contain only one print", do you mean one text line in the code file or one LOC/statement? – Ruslan – 2014-02-15T08:54:15.123
Every line of code can contain only one print – Vereos – 2014-02-15T15:07:51.327
does it have to pass a code review - suitable for production code? – Lance – 2014-02-17T19:37:26.420
Not really, it just has to meet the specs. – Vereos – 2014-02-17T21:59:53.110