19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Nested Loops<br />

The statements in the body of a For loop can be any <strong>Small</strong> <strong>Basic</strong> statement,<br />

including another For loop. Nesting is when you put one For loop inside<br />

another For loop (no birds are involved). Using nested loops allows you <strong>to</strong><br />

create iterations in two or more dimensions. This technique is important,<br />

and you can use it <strong>to</strong> solve a wide range of programming problems.<br />

To understand the idea of nested For loops, you’ll examine a program<br />

that causes your computer <strong>to</strong> “jump” four times and “clap” three times after<br />

each jump. Because the program needs <strong>to</strong> count two actions (jumps and<br />

claps), it needs <strong>to</strong> use two loops, as shown in Listing 13-7. The counter for<br />

the outer loop, j, runs from 1 <strong>to</strong> 4. The counter for the inner loop, c, runs<br />

from 1 <strong>to</strong> 3.<br />

1 ' NestedLoops.sb<br />

2 For j = 1 To 4 ' The jump counter<br />

3 TextWindow.Write("Jump " + j + ": ")<br />

4 For c = 1 To 3 ' The clap counter<br />

5 TextWindow.Write("Clap " + c + " ")<br />

6 EndFor<br />

7 TextWindow.WriteLine("")<br />

8 EndFor<br />

Listing 13-7: Nesting For loops<br />

In the first iteration of the outer loop (where j = 1), the inner loop<br />

repeats three times (for the three values of c); each time, it writes the word<br />

Clap followed by a space, the current value of c, and another space (line 5).<br />

When you nest For loops like this, the inner loop goes through all its iterations<br />

for each iteration of the outer loop. So the first iteration of the outer<br />

loop makes the program display Jump 1: Clap 1 Clap 2 Clap 3. When the<br />

inner loop ends, the program outputs an empty line (line 7) <strong>to</strong> move the<br />

cursor <strong>to</strong> the beginning of the next line, and the second iteration of the<br />

outer loop starts <strong>with</strong> j = 2. The inner loop runs again for c = 1, 2, and 3.<br />

This causes the program <strong>to</strong> display Jump 2: Clap 1 Clap 2 Clap 3. This continues,<br />

so the program displays Jump 3: Clap 1 Clap 2 Clap 3 and then Jump 4:<br />

Clap 1 Clap 2 Clap 3. Then the program ends. Perhaps your computer wants<br />

<strong>to</strong> be a cheerleader!<br />

Figure 13-6 helps <strong>to</strong> explain how the program works. The outer circle<br />

represents each time the outer loop runs: for example, at the <strong>to</strong>p of the<br />

outer circle, when j = 1 in the outer loop, the inner loop runs three times,<br />

where c = 1, c = 2, and c = 3. Follow the outer loop and think through each<br />

inner loop. Continue until you get all the way around the outer circle.<br />

Repeating For Loops 189

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!