faqts : Computers : Programming : Languages : Bbcbasic

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

BBCBASIC: Windows: Recursion: Can you write a program creating nested FOR NEXT loops?

May 4th, 2007 10:33
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 09 April 2007 - 07:57 pm ----------------------

BBCBASIC: Windows: Recursion: Can you write a program creating nested 
FOR NEXT loops?

===


 1. -To create the equivalent of 3 nested loops

      FOR I% = 1 TO 3

       FOR J% = 1 TO 3

        FOR K% = 1 TO 3

         PRINT I%, J%, K%

        NEXT K%

       NEXT J%

      NEXT I%

      you can e.g. create a recursive program

 2. -Create e.g. the following program

--- cut here: begin --------------------------------------------------

 DIM I%( 100 )
 :
 PROCForNext( 1, 3, 3 )
 REM PROCForNext( 1, 4, 4 )
 REM PROCForNext( 1, 5, 5 )
 REM PROCForNext( 1, 6, 6 )
 END
 :
 :
 :
 DEF PROCForNext( minI%, maxI%, nestMaxI% )
 PROCForNextSub( nestMaxI% )
 ENDPROC
 :
 DEF PROCForNextSub( nestMaxI% )
 LOCAL I%
 IF nestMaxI% = 0 THEN PROCForNextPrint( minI%, maxI% ) : ENDPROC
 FOR I% = minI% TO maxI%
   I%( nestMaxI% ) = I%
   PROCForNextSub( nestMaxI% - 1 )
 NEXT I%
 ENDPROC
 :
 DEF PROCForNextPrint( minI%, maxI% )
 LOCAL I%
 FOR I% = maxI% TO minI% STEP -1
   PRINT; I%( I% ); " ";
 NEXT I%
 PRINT
 ENDPROC
 :

--- cut here: end ----------------------------------------------------

 3. -If you run this it will show

--- cut here: begin --------------------------------------------------

 1 1 1
 1 1 2
 1 1 3
 1 2 1
 1 2 2
 1 2 3
 1 3 1
 1 3 2
 1 3 3
 2 1 1
 2 1 2
 2 1 3
 2 2 1
 2 2 2
 2 2 3
 2 3 1
 2 3 2
 2 3 3
 3 1 1
 3 1 2
 3 1 3
 3 2 1
 3 2 2
 3 2 3
 3 3 1
 3 3 2
 3 3 3

--- cut here: end ----------------------------------------------------

 4. -If you run it with

      PROCForNext( 1, 4, 4 )

     it will show the equivalent of

--- cut here: begin --------------------------------------------------

      FOR I% = 1 TO 4

       FOR J% = 1 TO 4

        FOR K% = 1 TO 4

         FOR L% = 1 TO 4

          PRINT I%, J%, K%, L%

         NEXT L%

        NEXT K%

       NEXT J%

      NEXT I%

--- cut here: end ----------------------------------------------------

 5. -If you run it with

      PROCForNext( 1, 5, 5 )

     it will show the equivalent of

--- cut here: begin --------------------------------------------------

      FOR I% = 1 TO 5

       FOR J% = 1 TO 5

        FOR K% = 1 TO 5

         FOR L% = 1 TO 5

          FOR M% = 1 TO 5

          PRINT I%, J%, K%, L%, M%

          NEXT M%

         NEXT L%

        NEXT K%

       NEXT J%

      NEXT I%

--- cut here: end ----------------------------------------------------

===

Internet: see also:

---

BBCBASIC: Windows: Combinatorics: Link: Can you give an overview of 
links?
http://www.faqts.com/knowledge_base/view.phtml/aid/45285/fid/768

----------------------------------------------------------------------