07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8. Loop Structures 51<br />

This program prints a pattern of x’s on the screen:<br />

x<br />

xx<br />

xxx<br />

xxxx<br />

xxxxx<br />

xxxxxx<br />

Let’s analyze this program. It has two loops: an outer loop with the variable row as the<br />

loop index, and within that an inner loop with the index variable xcount.<br />

———————————————————————————————————————<br />

x The inner or nested loop must be entirely inside the outer loop.<br />

———————————————————————————————————————<br />

Each time the outer loop goes through one big cycle, the inner loop goes through as many<br />

cycles as the current value of row. This creates the triangle pattern. As you can see, the<br />

first row has one x, the second has two, and so on.<br />

Note the empty PRINT statement just after the inner loop and just before the end of the<br />

outer loop. This second PRINT statement is carried out only at the end of a row. It tells<br />

<strong>True</strong> <strong>BASIC</strong> to start a new line. If it wasn’t there, the program would just print 21 x’s on<br />

one line.<br />

If you want to print more than one triangle, you’ll have to use three loops, not just two. Nest<br />

a new loop between the row and xcount loops. Notice how the indenting and blank lines<br />

help you keep track of which loop is which:<br />

! Print pattern of x’s.<br />

!<br />

FOR row = 1 to 6<br />

FOR triangle = 1 to 3<br />

! new loop starts here<br />

FOR xcount = 1 to row<br />

PRINT “x”;<br />

NEXT xcount<br />

PRINT,<br />

NEXT triangle<br />

! new PRINT with comma<br />

! new loop ends here<br />

PRINT<br />

NEXT row<br />

END

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

Saved successfully!

Ooh no, something went wrong!