Loop using GO TO - cobol

I need to write a "for loop" in COBOL without using the 'PERFORM ... THRU ...' structure. My idea is to add a paragraph that I can jump back to once certain conditions are met. Here is what I came up with:
PROGRAM-BEGIN.
PAR-A.
IF I <= 10 THEN
SET J TO 1
PAR-B.
IF J <= 10 THEN
DISPLAY ARRAY(I,J)
SET J UP BY 1
GO TO PAR-B
END-IF.
SET I UP BY 1
GO TO PAR-A
END-IF.
PROGRAM-DONE.
Clearly this doesn't work because writing in this way will incur a syntax error. Can anyone help me on this? I can only use IF and GO TO.

This is what you'd need with your existing structure. You've not shown the initialisation of I, but you'll need one. You've attempted to avoid an explicit termination condition/GO TO,
PROGRAM-BEGIN.
SET I TO 1
PAR-A.
IF I <= 10 THEN
SET J TO 1
ELSE
GO TO PROGRAM-DONE
END-IF
.
PAR-B.
IF J <= 10 THEN
DISPLAY ARRAY(I,J)
SET J UP BY 1
GO TO PAR-B
END-IF
SET I UP BY 1
GO TO PAR-A
.
PROGRAM-DONE.
Note the use of the full-stops/periods. You should adopt that for your code, you'll have fewer troubles.
That comma is also trying to disguise itself as a full-stop/period/mark on the screen, and why even include it if you are jamming everything up against each other:
DISPLAY ARRAY ( I J )
There, isn't that nicer?
From the start, work on your names. Use descriptive names. I and J are just plain dumb, and in some wonderful situations you will even confuse them with the number 1.
SET first-level-index
second-level-index TO 1
.
output-results.
IF second-level-index
NOT GREATER THAN 10
DISPLAY
">"
the-data
( first-level-index
second-level-index )
"<"
SET second-level-index UP BY 1
GO TO output-results
END-IF
IF first-level-index
NOT GREATER THAN 10
SET second-level-index TO 1
SET first-level-index UP BY 1
GO TO output-results
END-IF
.
Or
set-up-for-loop.
SET first-level-index TO 1
.
outer-loop.
SET second-level-index TO 1
.
inner-loop.
IF second-level-index
NOT GREATER THAN 10
DISPLAY
">"
the-data
( first-level-index
second-level-index )
"<"
SET second-level-index UP BY 1
GO TO inner-loop
END-IF
IF first-level-index
NOT GREATER THAN 10
SET first-level-index UP BY 1
GO TO outer-loop
END-IF
.
You'd give those paragrpah-names descibing the actual task.
Be aware that comparing indexes (your I and J) to literals requires some twists and turns for the compiler.

To fix the syntax error caused by putting a paragraph name inside
the if, you can use the GOTO to move the PARB paragraph out of the first IF statement:
PROGRAM-BEGIN.
PAR-A.
IF I <= 10 THEN
SET J TO 1
GOTO PAR-B
END-IF
PAR-B.
IF J <= 10 THEN
DISPLAY ARRAY(I,J)
SET J UP BY 1
GO TO PAR-B
END-IF.
SET I UP BY 1
GO TO PAR-A
PROGRAM-DONE.

Related

Selecting cases based on values in previous cases

I've got the following list in SPSS:
Subjekt Reactiontime correct/incorrect
1 x 1
1 x 0
1 x 1
1 x 0
I now want to select all rows/cases that follow AFTER "0" (in the column correct/incorrect) because I want to compute the mean of all reactiontimes that come after "0".
How can I do that in SPSS?
One way to do this would be to add a column that keeps track of whether the prior row was equal to 0 in your correct field and then calculate the mean Reactiontime of those cases.
First let's make a variable to flag cases we want included in the average.
* set prev_correct to 0 if the prior case was 0 .
IF (LAG(correct)=0) prev_correct=0 .
* else set to -1 .
RECODE prev_correct (SYSMIS=-1) .
EXE .
Now we can calculate the mean reaction time, splitting by our new variable.
MEANS Reactiontime BY prev_correct /CELLS MEAN .
Or, if we only want to output the mean when prev_correct=0 .
TEMP .
SELECT IF prev_correct=0 .
MEANS Reactiontime /CELLS MEAN .
Here's a shorter approach (though less generic than #user45392's full process):
if lag(correct)=0 ReactiontimeAfter0=Reactiontime.
now you can just run means ReactiontimeAfter0.

Finding the total from a single dimensional array - COBOL

I need to iterate through a 1D array and add all of the elements together to find the total. I must use a Perfrom ... Varying statement, this is what I have come up with so far.
perform 100-read-input-file
varying emp-rec-calls(ws-emp-total)
from 1 by ws-emp-total
until (ws-eof-flag = 'Y'
OR ws-array-counter > ws-array-max)
add emp-rec-calls(ws-emp-total) to ws-total-temp
The code for 100-read-input-file is simply
read input-file at end move 'y' to found-eof.
The problem I am currently getting is "Subscript out of range:" on this line "perform 100-read-input-file". All help is appretiated, thanks!
Let's analyze the code you provided:
perform 100-read-input-file
varying emp-rec-calls(ws-emp-total)
from 1 by ws-emp-total
until (ws-eof-flag = 'Y'
OR ws-array-counter > ws-array-max)
add emp-rec-calls(ws-emp-total) to ws-total-temp
This loop doesn't really make any sense. You are saying perform this loop varying occurance X of the array EMP-REC-CALLS from 1 by X until a flag that never gets set within the loop is equal to yes OR a counter you are not incrementing is greater than the array size.
I think you are trying to achieve something like this:
PERFORM VARYING WS-ARRAY-COUNTER
FROM 1 BY 1
UNTIL WS-ARRAY-COUNTER > WS-ARRAY-MAX
ADD EMP-REC-CALLS(WS-COUNTER) TO WS-TOTAL-TEMP
END-PERFORM
This will vary the counter WS-ARRAY-COUNTER by 1 every iteration of the loop (starting at 1) until that counter is greater than the max defined.

COBOL Beer on the Wall Program

I'm making the "99 Bottles" program, but with user input on how many to take down. I'm very new to COBOL and I'm definitely overlooking something simple or just completely thinking about this the wrong way.
The following is what I currently have:
IDENTIFICATION DIVISION.
PROGRAM-ID. HW.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 COUNTER PIC S99.
01 BOTTLES PIC Z9.
01 BOTTLES-REMAINING PIC Z9.
01 NUM PIC s9(02) VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING COUNTER FROM 99 BY NUM UNTIL COUNTER = 0
DISPLAY "How many bottles would you like to take down?"
ACCEPT NUM
MOVE COUNTER to Bottles
subtract NUM FROM COUNTER GIVING BOTTLES-REMAINING
DISPLAY SPACES
EVALUATE COUNTER
WHEN 1
DISPLAY " 1 bottle of beer on the wall, "
" 1 bottle of beer."
DISPLAY "Take one down and pass it around, "
"no more bottles of beer on the wall."
WHEN 2 Thru 99
DISPLAY BOTTLES " bottles of beer on the wall, "
BOTTLES " bottles of beer."
DISPLAY "Take one down and pass it around, "
BOTTLES-REMAINING
" bottles of beer on the wall."
END-EVALUATE
END-PERFORM
GOBACK.
I need to make the NUM clause negative in the following statement (or the data division) so it will subtract from the counter:
PERFORM VARYING COUNTER FROM 99 BY NUM UNTIL COUNTER = 0
I see a few issues here.
First, and this is from admittedly faded memory, but I seem to recall that the VARYING clause required a constant value for the delta. I don't think you can use an actual changing NUM to do this.
So your loop would be better off not using the VARYING clause and instead be something like (code here may not be syntactically correct COBOL, it's meant more to show intent and/or method):
set counter to 99
perform until counter = 0
blah blah blah then change counter
end perform
Second, your little ditty doesn't make sense any more if you're allowed to remove more than one bottle at a time. The statements for the third stanza of the rhyme should be modified similarly to the bottles-left stanza:
evaluate num
when 1
display "Take one down and pass it around, "
when 2 thru 99
display "Take ", num, " down and pass them around, "
end evaluate
And, finally, you probably want to avoid the situation where you remove more bottles than you have available (or less than one, for that matter). That can be done by silently enforcing those limits (clamping) immediately after getting the user input:
accept num
if num is less than one
set num to one
end if
if num is greater than counter
set num to counter
end if
You could also complain and require the user to enter a valid quantity but the easiest solution is probably just to clamp it.

Perform Varying Until sub equals 0

Just a quick question here, how would COBOL deal with the following statement?
MOVE ZERO TO WS-SUB-2.
And then later:
PERFORM A100
VARYING WS-SUB FROM 1 BY 1
UNTIL WS-SUB > WS-SUB-2.
Reason being is I have a perform varying until where 2 variables are compared, but "WS-SUB-2" can be zero. I just need to know if "A100" would ever be performed.
A100 is never executed.
PERFORM is a loop with an explicit loop counter, which means it knows about the sequencing of each iteration. What exactly happens, is this:
WS-SUB is set to 1
The expression WS-SUB > WS-SUB-2 is evaluated.
If WS-SUB is greater than WS-SUB-2, then break out of the loop en continue with the next statement.
Else, then perform the A100 procedure; and at last WS-SUB is incremented by 1.
Because at the very start WS-SUB is greater than WS-SUB-2, the perform statement is instantly terminated, thus A100 is never executed.
It is a little hard to understand what exactly it is that you want. Here a few suggestions:
If you want A100 to be executed at least once, you can use this:
IF WS-SUB-2 = ZERO
MOVE 1 TO WS-SUB-2
END-IF
If you want to execute A100 WS-SUB-2 + 1 times, then either change UNTIL WS-SUB > WS-SUB-2 TO UNTIL WS-SUB > WS-SUB-2 + 1 or change FROM 1 to FROM 0.
No, A100 won't be executed. Because PERFORM by default is WITH TEST BEFORE. So as soon as PERFORM detects WS-SUB (1) is greater than WS-SUB2 (0), it stops. If you want A100 to perform once, you can use PERFORM WITH TEST AFTER. And in this case, A100 will run once.

Nested Perform Loops in COBOL?

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.
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.
You have a period in the display INPUTC statement. Remove the period and you should be ok. The Period always ends a statement.

Resources