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.

Changing the Value of a Variable<br />

When you create a new variable in your program, you assign it an initial<br />

value using the assignment opera<strong>to</strong>r. You can also use the assignment<br />

opera<strong>to</strong>r <strong>to</strong> change the value of an existing variable, which wipes out the<br />

old value, like this:<br />

ferrets = 5<br />

ferrets = 15<br />

TextWindow.WriteLine("There are " + ferrets + " ferrets in my bed!")<br />

The first line in this example creates a new variable named ferrets and<br />

assigns 5 <strong>to</strong> it, but the second line changes its value <strong>to</strong> 15. So the WriteLine<br />

statement will output There are 15 ferrets in my bed!<br />

Behind the scenes, the variable ferrets points <strong>to</strong> a s<strong>to</strong>rage area in the<br />

computer’s memory. When you write ferrets = 15, you tell <strong>Small</strong> <strong>Basic</strong> <strong>to</strong> go<br />

in<strong>to</strong> the space in the memory reserved for ferrets and replace the 5 <strong>with</strong> a<br />

15. When you display the value of ferrets, you grab whatever’s s<strong>to</strong>red in the<br />

space at that moment.<br />

You can also add <strong>to</strong> a variable <strong>to</strong> change its value. Imagine you’re programming<br />

a game in which the player has <strong>to</strong> shoot attacking airplanes. When<br />

the player shoots an airplane, you want <strong>to</strong> increase their score (s<strong>to</strong>red in a<br />

variable named score) by five points. How would you update the score variable?<br />

Here’s one way:<br />

score = 10 ' Assumes the player already has 10 points<br />

temp = score + 5 ' temp = 10 + 5 (= 15)<br />

score = temp ' Now the player has 15 points<br />

The second line uses a temporary variable named temp <strong>to</strong> s<strong>to</strong>re the<br />

result of adding five <strong>to</strong> the current value of score. The value of temp is then<br />

assigned <strong>to</strong> score.<br />

But you can do this faster in one statement:<br />

score = score + 5<br />

Do you see how the same variable, score, is on both sides of the assignment?<br />

This statement adds 5 <strong>to</strong> the current value of the variable score and<br />

then s<strong>to</strong>res the result back in<strong>to</strong> the same variable. The old value of 10 is<br />

replaced by the new value, 15. See Figure 4-1.<br />

score = score + 5<br />

15<br />

New value<br />

of score<br />

= 10 + 5<br />

Old value<br />

of score<br />

Figure 4-1: Illustrating the statement<br />

score = score + 5<br />

46 Chapter 4

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

Saved successfully!

Ooh no, something went wrong!