Vadim Zaytsev aka @grammarware

BabyCobol: FizzBuzz

FizzBuzz is a kids game turned popular by its prominence in job interviews for software developers. This solution can also be found on Rosetta Code.

       IDENTIFICATION DIVISION.
       PROGRAM-ID.  FIZZBUZZ.
       DATA DIVISION.
       01 INT PICTURE IS 9(3).
       01 REM LIKE INT.
       01 TMP LIKE INT.
       PROCEDURE DIVISION.
           LOOP VARYING INT TO 100
               DIVIDE 3 INTO INT GIVING TMP REMAINDER REM
               IF REM = 0
               THEN DISPLAY "Fizz" WITH NO ADVANCING
      * NB: no END!
               DIVIDE 5 INTO INT GIVING TMP REMAINDER REM
               IF REM = 0
               THEN DISPLAY "Buzz" WITH NO ADVANCING
      * NB: again, no END!
               DIVIDE 15 INTO INT GIVING TMP REMAINDER REM
               IF REM = 0
               THEN DISPLAY ""
               ELSE DISPLAY INT
      * NB: still, no END!
           END.
      * Okay, this is an END but for the LOOP, not for IFs!

Having three unclosed IFs in a sequence leads to a highly ambiguous parse which only converges by witnessing the last END which is mandatory for LOOP. Hence, no dangling!


BabyCobol is a project by Dr. Vadim Zaytsev a.k.a. @grammarware. Page last updated in March 2024.
XHTML 1.1 CSS 3