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).