Programming Fundamentals/Loops/COBOL

From Wikiversity
Jump to navigation Jump to search

loops.cbl[edit | edit source]

*> This program demonstrates Until (While) and Varying (For) loop counting using user-designated
*> start, stop, and increment values.
*>
*> References:
*>     https://www.tutorialspoint.com/cobol/index.htm
*>     https://open-cobol.sourceforge.io/doc/gnucobol.html

IDENTIFICATION DIVISION.
PROGRAM-ID. LOOPS.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 BEGIN        PIC 9(2).
01 FINISH       PIC 9(2).
01 INCREMENT    PIC 9(2).
01 COUNTER      PIC 9(2).

PROCEDURE DIVISION.

MAIN.
    DISPLAY "Enter starting value:".
    ACCEPT BEGIN.
    DISPLAY "Enter ending value:".
    ACCEPT FINISH.
    DISPLAY "Enter increment value:".
    ACCEPT INCREMENT.
    
    DISPLAY "UNTIL-LOOP".
    SET COUNTER TO BEGIN.
    PERFORM UNTIL-LOOP UNTIL COUNTER > FINISH.
    
    DISPLAY "VARYING-LOOP"
    PERFORM VARYING-LOOP VARYING COUNTER FROM BEGIN BY INCREMENT UNTIL COUNTER > FINISH.
    STOP RUN.

UNTIL-LOOP.
    DISPLAY COUNTER.
    ADD INCREMENT TO COUNTER.
    
VARYING-LOOP.
    DISPLAY COUNTER.

Try It[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own COBOL compiler / interpreter / IDE.

See Also[edit | edit source]