Nested Loops

A bubble sort requires a nested For loop. As with the If statement, you can nest one loop inside another. Anytime your program needs to repeat a loop more than once, use a nested loop. Figure 12.3 shows an outline of a nested For loop. Think of the inside loop as looping faster than the outside loop. The inside loop iterates faster because the Inner variable goes from 1 to 10 in the inside loop before the outside loop's first iteration is completed. Because the outside loop does not repeat until the Next Outer statement, the inside For loop has a chance to finish. When the outside loop finally does iterate a second time, the inside loop starts all over again.

Note

Use any variable names for the outer and inner loop counter. The names ...

Get Absolute Beginner's Guide to Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.