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.

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

The FOR-NEXT loop is a structure in <strong>True</strong> <strong>BASIC</strong>, or a kind of framework that organizes<br />

other statements. The variable i in this program is called the index variable; it acquires<br />

a new value each time the loop runs.<br />

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

x The same index variable must appear in both the FOR statement and the<br />

NEXT statement.<br />

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

The statement(s) between the FOR and the NEXT statements are carried out (or executed)<br />

as many times as the loop is repeated. In this book, the statements inside the loop (in this<br />

case, the PRINT statement) are indented more than the FOR and NEXT statements. This<br />

is a matter of style; it’s not required in <strong>True</strong> <strong>BASIC</strong>, but it makes the program much easier<br />

to read.<br />

The loop alters the straight-line flow of control by repeating a group of statements. Such<br />

structures let you take advantage of the great power of computers.<br />

Step Size in a Loop<br />

The NEXT statement above added 1 to the index variable each time through the loop. You<br />

can make the NEXT statement add something other than 1 by putting your own step size<br />

in the FOR statement. For example, if you want a table of square roots in increments of<br />

one-tenth, you can use .1 as the step size.<br />

Open the demo program SQROOT from your <strong>True</strong> <strong>BASIC</strong> BRONZE <strong>Edition</strong> disk:<br />

! Square roots.<br />

!<br />

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

PRINT<br />

! Leave blank line<br />

FOR number = 0 to 1 step .1 ! From 0 to 1 in small steps<br />

PRINT number, Sqr(number) ! Print number & square root<br />

NEXT number<br />

END<br />

and run it:<br />

Number<br />

Square Root<br />

0 0<br />

.1 .31622777<br />

.2 .4472136<br />

.3 .547722256<br />

.4 .63245553

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

Saved successfully!

Ooh no, something went wrong!