Exemple – IF

Voici un programme utilisant le IF :

IDENTIFICATION DIVISION.
PROGRAM-ID.  IF.
AUTHOR.      LA COMMUNAUTE DU COBOL.

DATA DIVISION.
WORKING-STORAGE SECTION.

01  WS-VARIAVEIS.
    05  NUM1           PIC 9(001)  VALUE ZEROES.
    05  NUM2           PIC 9(001)  VALUE ZEROES.
    05  RESULT         PIC 9(002)  VALUE ZEROES.
    05  OPERATEUR      PIC X(001)  VALUE SPACES.

PROCEDURE DIVISION.

001-INIT.

    PERFORM 3 TIMES
       DISPLAY "Inserer le premier numero  : " WITH NO ADVANCING
       ACCEPT NUM1
       DISPLAY "Inserer le deuxieme numero : " WITH NO ADVANCING
       ACCEPT NUM2
       DISPLAY "Inserer un operateur (+ ou *) : " WITH NO ADVANCING
       ACCEPT OPERATEUR

       IF OPERATEUR = "+" THEN
          ADD NUM1 NUM2 GIVING RESULT
       END-IF

       IF OPERATEUR = "*" THEN
          MULTIPLY NUM1 BY NUM2 GIVING RESULT
       END-IF

       DISPLAY "Le resultat est = ", RESULT
    END-PERFORM.

    STOP RUN.