Small Basic – Multiple Nesting Levels


You can have more than two levels of nesting in Small Basic. The example in the Listing below shows you three levels of nesting. The flowchart in the Figure below explains the output of this program.

 

Listing:

P = 0   ' Pass number

For I = 1 To 2

  For J = 1 To 2

    For K = 1 To 2

      P = P + 1

      TextWindow.Write( "pass " + P + ": ")

      TextWindow.WriteLine( I + "," + J + "," + K )

    EndFor

  EndFor

EndFor

 

Output:

pass 1: 1,1,1

pass 2: 1,1,2

pass 3: 1,2,1

pass 4: 1,2,2

pass 5: 2,1,1

pass 6: 2,1,2

pass 7: 2,2,1

pass 8: 2,2,2

 

  

Figure: Flowchart that illustrates the output of the Listing above

 

The first loop from the Figure starts by setting I=1 (follow it to the left). The second loop runs two times: one for J=1 and then for J=2. For each of these passes, the third loop runs two times: one for K=1 and then for K=2. When the second loop completes, the first loop runs another time with I=2 (follow it to the right).

 

Leave a comment if you have any questions!

 

Head to http://blogs.msdn.com/SmallBasic to download it and learn all about it!

   

Small and Basically yours,

   - Ninja Ed & Majed Marji

Comments (1)

  1. Computers Today (part 1 of 6)

    blogs.msdn.com/…/computers-today.aspx

    …..

    CS SPOTLIGHT: Girls in computer programming… why it matters!!!

    blogs.msdn.com/…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

    Computational Thinking – Videos & Papers by Jeannette Wing

    blogs.msdn.com/…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx

Skip to main content