View previous topic :: View next topic
|
Author |
Message |
mambopras
New User
Joined: 11 Nov 2008 Posts: 52 Location: Hyderabad
|
|
|
|
Hi,
Recently I have tried a PERFORM code in a program.
WORKING STORAGE SECTION.
01 WS-TIME PIC 9 VALUE 7.
PROCEDURE DIVISION.
PERFORM WS-TIME TIMES
Statement 1...........
Statement 2...........
MOVE 4 TO WS-TIME
END-PERFORM.
The loop got executed 7 times even after changing the value of WS-TIME to 4. So, does this happen by the virtue of the PERFORM type we are using - Any other reason ?
Is there a way that this PERFORM type can be made to execute just 4 times by using any other option (not with a different PERFORM type though) ? |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
Just in case the COBOL manual does not address this issue, you can always generate the compiled code (procedure map I think) and see what is happening. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
At the top of the page is a link to "IBM Manuals". The first group of manuals is for COBOL.
Suggest you read all about the variations of the PERFORM. If you find something that is not clear in the documentation, post what you found and your doubt. Someone will be able to clarify. |
|
Back to top |
|
|
Dsingh29
Active User
Joined: 16 Dec 2008 Posts: 132 Location: IBM
|
|
|
|
No, this is not possible in this variant of PERFORM.
Looks to me, a simple interview question. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Quote: |
The loop got executed 7 times even after changing the value of WS-TIME to 4. So, does this happen by the virtue of the PERFORM type we are using - Any other reason ? |
You need to learn to read manuals -- link at the top of the page. The COBOL Language Reference manual clearly and explicitly describes your code in section 6.2.27.3 and if you had bothered to read it, you would not even have needed to post anything. |
|
Back to top |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Well, the format of the PERFORM, you've used, will execute a block of code WS-TIME number of times before returning control to the statement following the PERFORM.
Try this,
Code: |
WORKING-STORAGE SECTION.
01 WS-TIME PIC 9 VALUE 7.
01 WS-NO PIC 9 VALUE 0.
.
.
PROCEDURE DIVISION.
.
.
PERFORM WS-TIME TIMES
DISPLAY'>'WS-NO'<'
MOVE 4 TO WS-TIME
ADD +1 TO WS-NO
END-PERFORM |
you'll find that, WS-NO has the values 0, 1, 2, 3, 4, 5, 6 irrespective of that you move 4 to WS-TIME in between. COBOL is not looking at that, per the definition of in-line PERFORM, the bold text above. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
If you peform N times, Cobol will establish its' own little piece of storage to manage the counting to N. It does this before any of the statements within the PERFORM itself are actioned. Since you don't have any access to Cobol's little counter, you will be unable to affect the number of TIMES the thing is performed after it has already started.
And why would you want to? If you find yourself wanting to do that, it just means you've chosen the wrong sort of PERFORM control.
As Ph... Phil said, get the compiler to list the generated "pseudo assembler" code and it will all be there.
Every time, it will do it however many times you asked it to be done with the PERFORM and when that is all done, will continue with the next statement after the PERFORM statement as a whole. |
|
Back to top |
|
|
Jose Mateo
Active User
Joined: 29 Oct 2010 Posts: 121 Location: Puerto Rico
|
|
|
|
Good day to all!
Like everyone has mention that this is a in-lin perform and it will perform the block code the number of time from the initial value taken, is like if you had specified PERFORM 7 TIMES. Now if you want this block of code to be executed 4 time then you would use PERFORM UNTIL WS-TIME LESS THAN 0 and in your block code you would replace the 'MOVE 4 TO WS-TIME' with 'SUBTRACT 2 FROM WS-TIME'. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Jose Mateo wrote: |
Good day to all!
Like everyone has mention that this is a in-lin perform and it will perform the block code the number of time from the initial value taken, is like if you had specified PERFORM 7 TIMES. Now if you want this block of code to be executed 4 time then you would use PERFORM UNTIL WS-TIME LESS THAN 0 and in your block code you would replace the 'MOVE 4 TO WS-TIME' with 'SUBTRACT 2 FROM WS-TIME'. |
Whether the PERFORM is inline or not is irrelevant. It works exactly the same with PERFORM para/section. First time I've mentioned anything about that. Can't remember much mention from others either.
Code: |
PERFORM write-seperator-line number-of-seperators TIMES. |
The PERFORM... TIMES is designed to work when you know outside the body of the perform exactly the number of iterations you want.
Why you'd code your example, Jose, I don't know. There is nothing magic about 7 that you have to use it. If you want it four times, you can perform it four times.
If you don't know how many times you want to perform it, and you want it from zero to "a condition" times, then you use one of the other PERFORM formats as appropriate, with appropriate additional control clauses. |
|
Back to top |
|
|
Jose Mateo
Active User
Joined: 29 Oct 2010 Posts: 121 Location: Puerto Rico
|
|
|
|
Bill,
You are right and I won't disagree with you. Mambopras asked if there's another perform given the variable ws-time = 7, which would execute his block of code 4 times and I suggested PERFORM UNTIL. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
OK, Jose.
It's the weekend already, so enjoy it :-) |
|
Back to top |
|
|
|