19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15<br />

25<br />

Average = 17.5<br />

The program prompts the user <strong>to</strong> enter four numbers and press enter<br />

after each number. It reads these numbers, one by one, and saves them in<br />

four variables: n1, n2, n3, and n4 (lines 4–7). It then computes the average of<br />

these numbers, saves the average in the variable avg (line 8), and displays<br />

the result (line 9).<br />

Listing 6-6 shows a different way <strong>to</strong> write this program. Enter this program,<br />

and then run it. This time you’ll use just one variable named sum.<br />

1 ' Avg2.sb<br />

2 TextWindow.Write("Enter 4 numbers. ")<br />

3 TextWindow.WriteLine("Press after each one:")<br />

4 sum = TextWindow.ReadNumber()<br />

5 sum = sum + TextWindow.ReadNumber()<br />

6 sum = sum + TextWindow.ReadNumber()<br />

7 sum = sum + TextWindow.ReadNumber()<br />

8 TextWindow.WriteLine("Average = " + (sum / 4))<br />

Listing 6-6: Finding the average of four numbers using an accumula<strong>to</strong>r<br />

To understand how the program works, let’s say the user entered the<br />

numbers 10, 20, 15, and 25 in response <strong>to</strong> the prompt. So, in line 4, sum<br />

becomes 10. In line 5, the second number (20) is added <strong>to</strong> the first number<br />

(10) and saved <strong>to</strong> the sum variable (<strong>to</strong>taling 30). In lines 6–7, the third<br />

number (15) and fourth number (25) are added and saved <strong>to</strong> sum (<strong>to</strong>taling<br />

70). The program then displays the average, which is sum / 4, <strong>to</strong> the user<br />

(line 8).<br />

Because of how the sum variable keeps adding input <strong>to</strong> itself (or accumulating),<br />

it’s known as an accumula<strong>to</strong>r (also known as a running sum). (This<br />

might be similar <strong>to</strong> how you accumulate hairbands or Pokémon cards, but<br />

these numbers only take up computer memory and don’t clutter your room.)<br />

Reading Text<br />

Next, let’s write a simple program that makes silly sentences using the<br />

words in Shakespeare’s famous quote: “To be or not <strong>to</strong> be: that is the question.”<br />

You’ll ask the user <strong>to</strong> enter two verbs and a noun, and then you’ll use<br />

these entries <strong>to</strong> replace the words be, be, and question in Shakespeare’s quote.<br />

Listing 6-7 shows the complete program.<br />

1 ' Silly.sb<br />

2 TextWindow.Write("Please enter a verb: ")<br />

3 verb1 = TextWindow.Read()<br />

4<br />

5 TextWindow.Write("Please enter another verb: ")<br />

6 verb2 = TextWindow.Read()<br />

7<br />

8 TextWindow.Write("Now, please enter a noun: ")<br />

Getting User Input 79

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

Saved successfully!

Ooh no, something went wrong!