Archives par mot-clé : VARYING

Exemple – PERFORM VARYING UNTIL AFTER

Un bloc d’instruction ou un paragraphe/section sera exécuté dans PERFORM VARYING jusqu’à ce que la condition devienne vraie dans la phrase UNTIL.

Dans l’exemple ci-dessous, il y a trois DEPARTEMENTS et chaque département a deux EMPLOYES.

       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID.       PERFORM.                                      
       AUTHOR.           LA COMMUNAUTE DU COBOL.                      
       ENVIRONMENT DIVISION.                                            
    
       CONFIGURATION SECTION.                                           
       SPECIAL-NAMES.                                                   
               DECIMAL-POINT IS COMMA. 

       DATA DIVISION.

       WORKING-STORAGE SECTION.                                         

       01  WS-AUXILIARES.
           05 WS-DEPT-01       PIC 9(002) VALUE ZEROES.
           05 WS-DEPT-02       PIC 9(002) VALUE ZEROES.

       01  TABLE-DEPARTAMENT.
           05 DEPARTAMENT                OCCURS 3 TIMES.
              10 NOM-DEPT      PIC X(20).
              10 NOM-EMPL      PIC X(20) OCCURS 2 TIMES.

       PROCEDURE DIVISION.

           MOVE "FACTURATION"   TO NOM-DEPTO (1)
           MOVE "QUALITE"       TO NOM-DEPTO (2)
           MOVE "MARKETING"     TO NOM-DEPTO (3)

           MOVE "Carl"          TO NOM-EMPL (1 1)	
           MOVE "Albert"        TO NOM-EMPL (1 2)	
           MOVE "Augustin"      TO NOM-EMPL (2 1)	
           MOVE "Eduard"        TO NOM-EMPL (2 2)	
           MOVE "Antoine"       TO NOM-EMPL (3 1)	
           MOVE "Marie"         TO NOM-EMPL (3 2)  

           DISPLAY "--------------------------------------------------------------------------------------------"
           DISPLAY "En utilisant PERFORM VARYING UNTIL "
           DISPLAY "--------------------------------------------------------------------------------------------"
           
           PERFORM VARYING WS-DEPT-01 FROM 1 BY 1
                     UNTIL WS-DEPT-01 > 3
              PERFORM VARYING WS-DEPT-02 FROM 1 BY 1
                        UNTIL WS-DEPT-02 > 2
                 PERFORM PARAGRAF-1 THRU PARAGRAF-1-FIN
              END-PERFORM                                                     
           END-PERFORM                                                     
           
           DISPLAY "--------------------------------------------------------------------------------------------"
           DISPLAY "En utilisant PERFORM VARYING UNTIL AFTER"
           DISPLAY "--------------------------------------------------------------------------------------------"
			  
           PERFORM PARAGRAF-1 THRU PARAGRAF-1-FIN
                  VARYING WS-DEPT-01 FROM 1 BY 1
                          UNTIL WS-DEPT-01 > 3
                  AFTER   WS-DEPT-02 FROM 1 BY 1
                          UNTIL WS-DEPT-02 > 2.
           STOP RUN.

       PARAGRAFO-1.

           DISPLAY " DEPARTAMENT : "  NOM-DEPT (WS-DEPT-01)
                   " EMPLOYEES   : "  NOME-EMPL (WS-DEPT-01 WS-DEPT-02).

       PARAGRAF-1-FIN.
           EXIT.

Résultat du test

--------------------------------------------------------------------------------------------
En utilisant PERFORM VARYING UNTIL 
--------------------------------------------------------------------------------------------
       DEPARTAMENT: FACTURATION          EMPLOYEES   : Carl
       DEPARTAMENT: FACTURATION          EMPLOYEES   : Albert
       DEPARTAMENT: QUALITE              EMPLOYEES   : Augustin
       DEPARTAMENT: QUALITE              EMPLOYEES   : Eduard
       DEPARTAMENT: MARKETING            EMPLOYEES   : Antoine
       DEPARTAMENT: MARKETING            EMPLOYEES   : Marie
--------------------------------------------------------------------------------------------
En utilisant PERFORM VARYING UNTIL AFTER
--------------------------------------------------------------------------------------------
       DEPARTAMENT: FACTURATION          EMPLOYEES   : Carl
       DEPARTAMENT: FACTURATION          EMPLOYEES   : Albert
       DEPARTAMENT: QUALITE              EMPLOYEES   : Augustin
       DEPARTAMENT: QUALITE              EMPLOYEES   : Eduard
       DEPARTAMENT: MARKETING            EMPLOYEES   : Antoine
       DEPARTAMENT: MARKETING            EMPLOYEES   : Marie