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.

50 BRONZE <strong>Edition</strong> <strong>Guide</strong><br />

! Square roots.<br />

!<br />

PRINT “Number”, “Square Root” ! Print labels<br />

PRINT<br />

! Leave blank line<br />

FOR number = 10 to 5 step -1 ! Go from 10 down to 5<br />

PRINT number, Sqr(number)<br />

NEXT number<br />

PRINT number<br />

END<br />

and run it again:<br />

Number<br />

Square Root<br />

! Print number & square root<br />

10 3.1622777<br />

9 3<br />

8 2.8284271<br />

7 2.6457513<br />

6 2.4494897<br />

5 2.236068<br />

4<br />

As you can see, number equals 4 after the loop ends.<br />

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

x A FOR-NEXT loop always leaves the index variable with the first value<br />

that fails the end test.<br />

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

Nested Loops<br />

You may use any <strong>True</strong> <strong>BASIC</strong> statements inside a FOR-NEXT loop, even another loop.<br />

Some problems are best solved by using loops inside loops, that is, nested loops.<br />

As an illustration, open the demo program EXES:<br />

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

!<br />

FOR row = 1 to 6<br />

FOR xcount = 1 to row<br />

PRINT “x”;<br />

NEXT xcount<br />

PRINT<br />

NEXT row<br />

END

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

Saved successfully!

Ooh no, something went wrong!