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.

Try changing the value in karenBears and running the program again<br />

<strong>to</strong> see what happens. Neat, huh? Changing the value of karenBears will also<br />

change the value of lindaBears. Variables can make your programming life<br />

so much easier!<br />

Let’s check out some other important concepts you need <strong>to</strong> know for<br />

when you want <strong>to</strong> use your own variables.<br />

Assigning Expressions <strong>to</strong> Variables<br />

Arithmetic expressions are combinations of variables, opera<strong>to</strong>rs, and numbers.<br />

They can be constant numbers (such as 3, 6.8, or –10), arithmetic operations<br />

(such as 3 + 6 or 10 / 3), or algebraic expressions (karenBears / 2). Evaluating<br />

an arithmetic expression in <strong>Small</strong> <strong>Basic</strong> is just like evaluating an expression<br />

in math. For example, the expression 4 * 3 + 6 / 2 is evaluated as (4 × 3 + 6<br />

÷ 2) = 12 + 3 = 15. You can also use parentheses in expressions <strong>to</strong> decide the<br />

order of operations.<br />

You can set variables <strong>to</strong> the result of an arithmetic expression using<br />

an assignment statement. Your program grabs the value <strong>to</strong> the right of the<br />

equal sign and assigns that value <strong>to</strong> the variable on the left of the equal<br />

sign. You already did this in Listing 4-1; let’s build on that knowledge and<br />

write some more variables set <strong>to</strong> arithmetic expressions!<br />

Here’s an example:<br />

barbies = 5<br />

' You have 5 Barbies<br />

ponies = barbies + 7<br />

' You have 7 more My Little Ponies than Barbies<br />

donate = (barbies * ponies) / 10 ' Total <strong>to</strong>ys you need <strong>to</strong> donate<br />

When you run this program, the variables barbies, ponies, and donate are<br />

5, 12, and 6, respectively. Time <strong>to</strong> donate 6 <strong>to</strong>ys!<br />

You need <strong>to</strong> set the variable on the left of an assignment opera<strong>to</strong>r,<br />

as you’ve seen in every assignment example so far. So this statement is<br />

incorrect:<br />

5 = barbies ' This is backwards, like the Twilight Zone<br />

Try running it yourself <strong>to</strong> see if you get an error!<br />

Passing Variables <strong>to</strong> Methods<br />

A method’s arguments can be constants, variables, or even expressions. For<br />

example, the argument <strong>to</strong> WriteLine() in the following statement is an arithmetic<br />

expression:<br />

TextWindow.WriteLine((3 * x + y) / (x - y))<br />

If x = 7 and y = 5, this statement displays 13 on the screen. See if you<br />

can figure out how <strong>to</strong> write and run this code. Remember <strong>to</strong> set x and y<br />

first!<br />

Using Variables 45

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

Saved successfully!

Ooh no, something went wrong!