View previous topic :: View next topic
|
Author |
Message |
sandeep.tikoo
New User
Joined: 07 Oct 2006 Posts: 6 Location: Bangalore
|
|
|
|
how to loop within loop in cobol |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
Please explain in more detail what you are trying to accomplish. There are many implementations of imbedded loops.
Dave |
|
Back to top |
|
|
sandeep.tikoo
New User
Joined: 07 Oct 2006 Posts: 6 Location: Bangalore
|
|
|
|
actually all i want to do is make a multiplication table like
2*1=2 to 10*1=10,
where 2,1 must be variables like i,j i must be constant from 1 to 10 and then j should increase by 1 again i from 1 to 10 and j=2, to j should also go to 10...like this |
|
Back to top |
|
|
IQofaGerbil
Active User
Joined: 05 May 2006 Posts: 183 Location: Scotland
|
|
|
|
Something like this?
Perform Para1 varying i from 1 by 1 until i > 10
Para1
perform Para2 varying j from 2 by 1 until j > 10
Para2
compute ans = j * i |
|
Back to top |
|
|
DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
|
|
|
|
sandeep.tikoo,
Here are a couple of ways to implement you loops with ?PERFORM? statements.
In format 1 you can vary both variables within the same perform, but I prefer format 2 using two perform statements. Here is why. Although COBOL allows for the varying of multiple variables within the same perform, and this is probably the answer that is being looked for in an academic setting, once you get to the real world of programming, you won?t be varying multiple variables very often. My feeling is, that, at 2 a.m., when your program abends in a production environment, and the on call person for production problems has never looked at your program before, is not the time to re-acquaint themselves with a format that is seldom used.
Code: |
WORKING-STORAGE SECTION.
01 I PIC S9(5) COMP-3.
01 J PIC S9(5) COMP-3.
01 RESULT PIC 9(5).
LINKAGE SECTION.
PROCEDURE DIVISION.
PROGRAM-START.
DISPLAY 'FORMAT 1'.
PERFORM COMPUTE-DISPLAY-RESULT THRU COMPUTE-DISPLAY-EXIT
VARYING I FROM 1 BY 1 UNTIL I > 10
AFTER J FROM 1 BY 1 UNTIL J > 10.
DISPLAY 'FORMAT 2'.
PERFORM
VARYING I FROM 1 BY 1 UNTIL I > 10
PERFORM
VARYING J FROM 1 BY 1 UNTIL J > 10
PERFORM COMPUTE-DISPLAY-RESULT
THRU COMPUTE-DISPLAY-EXIT
END-PERFORM
END-PERFORM.
GOBACK.
COMPUTE-DISPLAY-RESULT.
COMPUTE RESULT = I * J.
DISPLAY 'I = ' I ' J = ' J ' RESULT = ' RESULT.
COMPUTE-DISPLAY-EXIT.
EXIT.
|
|
|
Back to top |
|
|
|