15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

144<br />

Declaring Variables<br />

Other programmers prefer to use uppercase letters at the beginning of each<br />

new word, such as<br />

SalesTaxFor2008;<br />

You can always mix and match both conventions if you want, such as<br />

SalesTaxFor_2008;<br />

No matter which naming style you prefer, it’s best to stick with one style to<br />

prevent your program from looking too confusing with so many different<br />

variable naming styles all over the place.<br />

Every variable needs a unique name. If you try to give two variables identical<br />

names, the computer gets confused and refuses to run your program.<br />

In some languages, such as the curly bracket family, which includes C, C++,<br />

and Java, variable names are case-sensitive. There<strong>for</strong>e, a variable named<br />

salestax is considered completely different than a SalesTax variable.<br />

Creating variables in a command<br />

The simplest way to create a variable is when you need it in a command.<br />

Suppose you have a program that asks the user <strong>for</strong> his annual salary, such as<br />

the following command (written in the Python <strong>programming</strong> language):<br />

salary = input(“What is your annual salary?”)<br />

This command displays, “What is your annual salary?” on-screen<br />

and waits <strong>for</strong> the user to type in a number.<br />

As soon as the user types in a number, such as 20000, the program needs a<br />

variable to store this number. In this example, the program creates a salary<br />

variable and stores the number 20000 in that variable.<br />

Creating variables whenever you need them in a command is simple and<br />

convenient — and potentially troublesome if you aren’t careful. The biggest<br />

problem is that when you create a variable within a command, that variable<br />

can store any type of data.<br />

Variables can hold two types of data:<br />

✦ Numbers, which are typically used to represent quantities or measurements.<br />

✦ Text (sometimes called strings, as in “text strings”) is used to represent<br />

non-numeric data, such as names or mailing addresses.

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

Saved successfully!

Ooh no, something went wrong!