View previous topic :: View next topic
|
Author |
Message |
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
I have a doubt in PERFORM VARYING...
Does changing the counter variable inside the execution range affect the no of the iterations?
Code: |
PERFORM 0100-PARA VARYING K FROM 1 BY 1 UNTIL K > 10.
.
.
.
0100-PARA.
ADD 2 TO K.
|
Does changing K in the para range affect the no of iterations?
I am currently not having access to mainframe. Otherwise I would have tested it myself.
Thanks,
Ajay |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
Don't mess with your look varbiables outside of the control statement if you can help it. Otherwise use a continue/no continue flag and just set to no when you want to stop. But adding different values than the BY 1 statement makes for crappy tracing when someone else tries to figure out what is going on. |
|
Back to top |
|
|
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
I know it is not a good way of programming. But I had taken one test and these type of questions were there.
Thanks,
Ajay |
|
Back to top |
|
|
murmohk1
Senior Member
Joined: 29 Jun 2006 Posts: 1436 Location: Bangalore,India
|
|
|
|
Ajay,
Quote: |
But I had taken one test and these type of questions were there. |
Good to you had agreed the fact. Many of the programmers overlook the add statement and chooses option which has 10 value. Just to confuse you, they will provide such questions.
As suggested, dont mess with the control variables. |
|
Back to top |
|
|
abhishekmdwivedi
New User
Joined: 22 Aug 2006 Posts: 95 Location: india
|
|
|
|
Ajay,
The variables used under loops for iteration purpose when manipulated under the performing para or statements are not reflected in the no. of iterations performed.
As the loop doesn't allow the control of the iteration variable to be used under the looped statements or para.
So,
Code: |
PERFORM 0100-PARA VARYING K FROM 1 BY 1 UNTIL K > 10.
.
.
.
0100-PARA.
ADD 2 TO K.
|
will iterate 9 times irrespective of the value assigned to variable K. |
|
Back to top |
|
|
ksk
Active User
Joined: 08 Jun 2006 Posts: 355 Location: New York
|
|
|
|
Hi Abhishe,
9 is wrong if K wil not be affected by the the ADD statment. |
|
Back to top |
|
|
abhishekmdwivedi
New User
Joined: 22 Aug 2006 Posts: 95 Location: india
|
|
|
|
Well KSK,
if you check.
Quote: |
variables used under loops for iteration purpose when manipulated under the performing para or statements are not reflected in the no. of iterations performed.
As the loop doesn't allow the control of the iteration variable to be used under the looped statements or para.
VARYING K FROM 1 BY 1 UNTIL K > 10
|
That's what I have written. |
|
Back to top |
|
|
ksk
Active User
Joined: 08 Jun 2006 Posts: 355 Location: New York
|
|
|
|
Hi Abhishek,
You are right. K starts from 1 and increments by 1.
When K value becomes 10, conditon becomes false, so it would be executed. when K becomes 11, i.e. 10 times, condition becomes true and control comes out of the perform.
So, it perfroms 10 times.
That's what I have thought and written. |
|
Back to top |
|
|
abhishekmdwivedi
New User
Joined: 22 Aug 2006 Posts: 95 Location: india
|
|
|
|
KSK,
You are right man for
Quote: |
So, it perfroms 10 times. |
Probably would have misread it.
My Actual focus was on the part
Quote: |
variables used under loops for iteration purpose when manipulated under the performing para or statements are not reflected in the no. of iterations performed.
As the loop doesn't allow the control of the iteration variable to be used under the looped statements or para.
|
Thanks man. |
|
Back to top |
|
|
ksk
Active User
Joined: 08 Jun 2006 Posts: 355 Location: New York
|
|
|
|
Abhishek,
Quote: |
Probably would have misread it.
My Actual focus was on the part
|
No problem. Just I wanted to write the correct number of times.
KSK |
|
Back to top |
|
|
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
Quote: |
variables used under loops for iteration purpose when manipulated under the performing para or statements are not reflected in the no. of iterations performed.
As the loop doesn't allow the control of the iteration variable to be used under the looped statements or para.
|
Code: |
0100-PARA.
ADD 2 TO K |
Does it mean ADD 2 TO K does not change the control variable K in the 0100-PARA, or the compiler neglects this statement ?
Thanks,
Ajay
[/code] |
|
Back to top |
|
|
rahul.banik
New User
Joined: 23 Jan 2007 Posts: 16 Location: Mysore
|
|
|
|
Hi Ajay,
Better way to check this is just display the 'k' within the 0100-PARA and
after the Perform stament.I think ADD 2 TO K will affect the no. of iterations. |
|
Back to top |
|
|
Douglas Wilder
Active User
Joined: 28 Nov 2006 Posts: 305 Location: Deerfield IL
|
|
|
|
The best way to know the answer is to test it.
Code: |
********************************* TOP OF DATA **********************************
AFTER ADD K = 003
AFTER ADD K = 006
AFTER ADD K = 009
AFTER ADD K = 012
******************************** BOTTOM OF DATA ******************************** |
Just as I thought, the add does modify the counter and the perform is done 4 times. |
|
Back to top |
|
|
akkireddy
New User
Joined: 13 Jul 2007 Posts: 1 Location: hyd
|
|
|
|
no change in that itterations... |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
This is flat out wrong.... did you even test this?
[quote]
The variables used under loops for iteration purpose when manipulated under the performing para or statements are not reflected in the no. of iterations performed.
As the loop doesn't allow the control of the iteration variable to be used under the looped statements or para.
[/code]
Douglas is correct as he shows with his test restuls. It does change the number of iterations. Cobol by default treats all working storage fields as global. Since K is a global working storage field, any modification of it will affect another part of the program.
Instead of 10 iterations, you will see only 4 iterations. |
|
Back to top |
|
|
ksk
Active User
Joined: 08 Jun 2006 Posts: 355 Location: New York
|
|
|
|
Hi,
Yes, douglas is correct. Perform is done only 4 times. Modifying the counter afftected the number of iterations.
Code: |
IN 0100-PARA AFTER ADD K=03
IN 0100-PARA AFTER ADD K=06
IN 0100-PARA AFTER ADD K=09
IN 0100-PARA AFTER ADD K=12
|
|
|
Back to top |
|
|
JAGAN_SRIRAM
New User
Joined: 21 Jul 2007 Posts: 4 Location: INDIA
|
|
|
|
no the value of the variable will not be changed until the perform is completed. explanation for this question is available in ibm interview questions |
|
Back to top |
|
|
ksk
Active User
Joined: 08 Jun 2006 Posts: 355 Location: New York
|
|
|
|
Hi,
Following is the code:
Code: |
PROCEDURE DIVISION.
PERFORM 0100-PARA VARYING K FROM 1 BY 1 UNTIL K > 10.
DISPLAY 'AFETR PERFORM K1=' K1.
STOP RUN.
0100-PARA.
ADD 1 TO K1.
ADD 2 TO K.
DISPLAY 'IN 0100-PARA AFTER ADD K=' K.
|
Following is the result:
Code: |
IN 0100-PARA AFTER ADD K=03
IN 0100-PARA AFTER ADD K=06
IN 0100-PARA AFTER ADD K=09
IN 0100-PARA AFTER ADD K=12
AFETR PERFORM K1=04
|
KSK |
|
Back to top |
|
|
ahalyah
New User
Joined: 13 Dec 2007 Posts: 25 Location: india
|
|
|
|
Hi,
i also agree with Douglas |
|
Back to top |
|
|
Douglas Wilder
Active User
Joined: 28 Nov 2006 Posts: 305 Location: Deerfield IL
|
|
|
|
JAGAN_SRIRAM, Do you believe ANY Web sight more than what the computer acutlly does? Try it yourself! What does the computer actually do? I already took the few minute to test it and posted my results.
If you provide a link we can check if you are misunderstanding the interview question or answer or if the question or answer is actualy wrong.
Of course, if that is the answer the interviewer is looking for, you can decide for yourself if you want to give the interviewer the correct answer or the answer they are looking for. It is not always a good idea to try to educate an interviewer during the interview, even if they are wrong. |
|
Back to top |
|
|
Ajay Baghel
Active User
Joined: 25 Apr 2007 Posts: 206 Location: Bangalore
|
|
|
|
Thanks everybody for such a prompt discussion.
As mentioned in my original message, i don't have access... so i could not test it.
I conclude from the test results that :
"Changing the iteration variable within the range of PERFORM does affect the final no of iterations. Also the iteration variable gets modified."
Thanks.
Ajay |
|
Back to top |
|
|
|