IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

how to loop within loop in cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sandeep.tikoo

New User


Joined: 07 Oct 2006
Posts: 6
Location: Bangalore

PostPosted: Mon Oct 09, 2006 7:01 pm
Reply with quote

how to loop within loop in cobol
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Mon Oct 09, 2006 7:06 pm
Reply with quote

Please explain in more detail what you are trying to accomplish. There are many implementations of imbedded loops.

Dave
Back to top
View user's profile Send private message
sandeep.tikoo

New User


Joined: 07 Oct 2006
Posts: 6
Location: Bangalore

PostPosted: Mon Oct 09, 2006 7:11 pm
Reply with quote

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
View user's profile Send private message
IQofaGerbil

Active User


Joined: 05 May 2006
Posts: 183
Location: Scotland

PostPosted: Mon Oct 09, 2006 8:12 pm
Reply with quote

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
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Mon Oct 09, 2006 8:21 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
No new posts Generate random number from range of ... COBOL Programming 3
Search our Forums:

Back to Top