19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

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

The program uses a variable named sum <strong>to</strong> hold the running <strong>to</strong>tal<br />

(this variable is usually called an accumula<strong>to</strong>r). The program starts by initializing<br />

sum <strong>to</strong> 0 (line 2). Then a For loop <strong>with</strong> a loop counter named N runs<br />

from 1 <strong>to</strong> 10 (line 3). During each iteration, the program adds the value<br />

of N <strong>to</strong> the accumula<strong>to</strong>r by using the statement at line 4. This statement<br />

adds the current value of N <strong>to</strong> the current value of sum and s<strong>to</strong>res the result<br />

back in<strong>to</strong> sum. After the first iteration, sum is 1 (0 + 1); after the second iteration,<br />

sum is 3 (1 + 2); after the third iteration, sum is 6 (3 + 3); and so on.<br />

When the loop ends, the program displays the value of the sum variable on<br />

line 6: sum = 55.<br />

TRY IT OUT 13-3<br />

When the great mathematician Carl Gauss first went <strong>to</strong> school, his teacher asked<br />

the class <strong>to</strong> find the sum of all the numbers between 1 and 100, that is, 1 + 2 +<br />

3 + 4 + . . . + 100. Gauss <strong>to</strong>ok one look at the problem and immediately put his<br />

answer on the teacher’s desk. The teacher was amazed—Gauss was right! Write<br />

a program <strong>to</strong> find the answer that Gauss worked out in his head. Of course, Gauss<br />

didn’t use <strong>Small</strong> <strong>Basic</strong>, but he did find a shortcut. Can you figure out his secret<br />

method?<br />

Formatting Your Output<br />

The way you display the output of a<br />

program is often just as important as<br />

the information you display. If the output<br />

is difficult <strong>to</strong> read, people won’t be<br />

able <strong>to</strong> understand what the information<br />

means. A well-laid-out display is an<br />

essential part of your program design,<br />

but getting the formatting right can be Figure 13-3: The output of<br />

tedious. To make it easier, you can use SquareTable.sb<br />

For loops. For example, let’s use a For<br />

loop <strong>to</strong> write a program that outputs the<br />

squares of 1 <strong>to</strong> 5 in a table format (see<br />

Figure 13-3).<br />

Enter and run the program in Listing 13-3.<br />

1 ' SquareTable.sb<br />

2 TextWindow.Title = "Table of Squares"<br />

3 TextWindow.WriteLine(" Number Square")<br />

4 TextWindow.WriteLine("======== =========")<br />

5<br />

6 For N = 1 To 5<br />

7 TextWindow.CursorLeft = 3 ' Moves <strong>to</strong> middle of col 1<br />

8 TextWindow.Write(N) ' Writes the number<br />

Repeating For Loops 185

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

Saved successfully!

Ooh no, something went wrong!