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.

Note<br />

Most other programming languages would report a syntax error because the expression<br />

x / 10 in line 2 uses a variable x that hasn’t been defined yet in the program.<br />

<strong>Small</strong> <strong>Basic</strong> lets you define variables anywhere in your program; just don’t expect <strong>to</strong><br />

get away <strong>with</strong> it in other languages.<br />

Be careful when you order your statements because <strong>Small</strong> <strong>Basic</strong> runs<br />

<strong>to</strong>p <strong>to</strong> bot<strong>to</strong>m. Make sure you define your variables before you use them for<br />

the first time.<br />

TRY IT OUT 4-5<br />

What’s the output of this program? Explain what you see.<br />

TextWindow.WriteLine("Before: x = " + x + " and y = " + y)<br />

x = 10<br />

y = 10<br />

TextWindow.WriteLine("After: x = " + x + " and y = " + y)<br />

<strong>Program</strong>ming Challenges<br />

If you get stuck, check out http://nostarch.com/smallbasic/ for the solutions<br />

and for more resources and review questions for teachers and students.<br />

1. Do you like knock-knock jokes? Try the following program <strong>to</strong> see how <strong>to</strong><br />

tell them in <strong>Small</strong> <strong>Basic</strong>! Which program lines do you need <strong>to</strong> change<br />

<strong>to</strong> tell a different joke? Make the change and run the program again <strong>to</strong><br />

see what happens:<br />

' KnockKnock.sb<br />

' <strong>Small</strong> <strong>Basic</strong> can tell knock-knock jokes!<br />

name = "Orange"<br />

reply = "you going <strong>to</strong> answer the door?"<br />

TextWindow.WriteLine("Knock Knock")<br />

TextWindow.WriteLine("Who's there?")<br />

TextWindow.WriteLine(name)<br />

TextWindow.WriteLine(name + " who?")<br />

TextWindow.WriteLine(name + " " + reply)<br />

TextWindow.WriteLine("")<br />

2. Translate this pseudocode in<strong>to</strong> a <strong>Small</strong> <strong>Basic</strong> program:<br />

a. Set quantity <strong>to</strong> 10.<br />

b. Set item price <strong>to</strong> 15 dollars.<br />

c. Compute <strong>to</strong>tal price by multiplying quantity by item price.<br />

d. Display <strong>to</strong>tal price.<br />

54 Chapter 4

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

Saved successfully!

Ooh no, something went wrong!