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.

TRY IT OUT 4-3<br />

Write a program that evaluates the following expression. Try taking a couple different<br />

approaches. Try doing it while dancing! Don’t be shy!<br />

1 + 7<br />

9 8<br />

1 + 6<br />

5 7<br />

Using Variables <strong>to</strong> Solve Problems<br />

People often solve problems <strong>with</strong>out really thinking through every step of<br />

the process. But computers can’t do this: they need you <strong>to</strong> think through<br />

each step for them (at least until The Termina<strong>to</strong>r or The Matrix come true).<br />

That’s why it takes some planning <strong>to</strong> use a computer <strong>to</strong> solve a problem.<br />

When developing a solution <strong>to</strong> a programming problem, you should do this:<br />

1. Understand what the problem is.<br />

2. Design a solution.<br />

3. Write the program.<br />

4. Test the program <strong>to</strong> make sure it works as you expect it <strong>to</strong>.<br />

Let’s say you want <strong>to</strong> create a program that computes the area of a circle<br />

<strong>with</strong> a given radius. To solve the problem, you need <strong>to</strong> answer these basic<br />

questions:<br />

1. What do you need the program <strong>to</strong> output?<br />

2. What input do you need, and where will the program get this input from?<br />

3. What processing will the program have <strong>to</strong> do <strong>to</strong> turn the input in<strong>to</strong> the<br />

output?<br />

For this problem, here’s how you’d answer those questions:<br />

1. Your program needs <strong>to</strong> output the area of a circle. It’ll show this output<br />

<strong>to</strong> the user in the text window.<br />

2. This program needs a single input from your user: the radius of the<br />

circle.<br />

3. Your program will need <strong>to</strong> compute the area using this formula, which<br />

might be familiar from math class:<br />

area = π × (radius) 2<br />

Note<br />

The Greek letter π, pronounced pi, is a special number that can be rounded <strong>to</strong><br />

3.1416. We gave you free pi!<br />

Using Variables 51

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

Saved successfully!

Ooh no, something went wrong!