Nested Perform Loops in COBOL?
Asked Answered
M

2

5

Why can I not do this nested perform loop in COBOL?

If I put END-PERFORM. in any line sooner than where I have the last one just before EXIT PROGRAM - it works. But I need the program to display the INPUT C value every time. in the outer perform loop. Its driving me nuts.

PROCEDURE DIVISION USING INPUTC CIPHER.
COMPUTE CIPHERMAX = CIPHER.
MULTIPLY -1 BY CIPHER
---> PERFORM VARYING CIPHER FROM 0 BY 1
UNTIL CIPHERMAX = CIPHER
    DISPLAY 'This is loop number: ' CIPHER
    INSPECT INPUTC CONVERTING
    "avcdefghijklmnopqrstuvwxyz" to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    COMPUTE CONVERTNUM = FUNCTION MOD (CIPHER, 26)
    INSPECT FUNCTION REVERSE(INPUTC) TALLYING LENGTHNUM FOR LEADING SPACES
    COMPUTE LENGTHNUM = LENGTH OF CIPHER - LENGTHNUM

 ---> PERFORM UNTIL SENTRY = LENGTHNUM

            IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
             MOVE FUNCTION CHAR((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) - 26) TO RECHAR
              ELSE
             MOVE FUNCTION CHAR(FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) TO RECHAR
             END-IF  
              IF (((FUNCTION ORD(INPUTC(SENTRY:1))) >= (FUNCTION ORD('A'))) AND 
             ((FUNCTION ORD(INPUTC(SENTRY:1))) <= (FUNCTION ORD('Z'))))
             IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
             ELSE
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
             END-IF
          ELSE
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY INPUTC(SENTRY:1)
          END-IF

          COMPUTE SENTRY = SENTRY + 1
        --->  END-PERFORM
    DISPLAY INPUTC.
    COMPUTE LOOPI = LOOPI + 1
    --->END-PERFORM.
EXIT PROGRAM.
END PROGRAM SOLVE.
Marianelamariani answered 25/4, 2013 at 17:13 Comment(2)
You have an IF with its ELSE having identical code. You also have an ELSE with an INSPECT replacing a character by itself. Why are you using INSPECT ... REPLACING for one-byte fields? Why not just MOVE? What is LOOPI for? Why not ADD 1 TO SENTRY instead of COMPUTE? You MULTIPLY -1, SUBTRACT ... FROM ZERO will be faster. But then you immediately set CIPHER to 0 in the PERFORM, so why negate anyway? I suspect there is more, but you've made the whole thing difficult to follow with FUNCTION upon FUNCTION.Bilbrey
Also, in avcdefghijklmnopqrstuvwxyz, you may have meant abc...Valerle
U
9

That nasty scope terminating period after DISPLAY INPUTC. is terminating the scope of the nested PERFORM statements. Get rid of the period and all should work fine.

The only periods you should ever use in the Procedure Division when coding a program to COBOL-85 standard are the ones required to terminate section and paragraph headers and another one to terminate the current paragraph, section or program.

Udele answered 25/4, 2013 at 19:51 Comment(0)
B
4

You have a period in the display INPUTC statement. Remove the period and you should be ok. The Period always ends a statement.

Blood answered 12/7, 2013 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.