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.

162<br />

Storing Data with the Assignment Operator<br />

Programming languages typically provide two types of data manipulation<br />

commands:<br />

✦ Operators are usually symbols that represent simple calculations, such<br />

as addition (+) or multiplication (*).<br />

✦ Functions are commands that per<strong>for</strong>m more sophisticated calculations,<br />

such as calculating the square root of a number.<br />

Unlike operators, which are usually symbols, functions are usually short<br />

commands, such as SQRT (square root).<br />

By combining both operators and functions, you can create your own commands<br />

<strong>for</strong> manipulating data in different ways.<br />

Storing Data with the Assignment Operator<br />

The simplest operator that almost every <strong>programming</strong> language has is the<br />

assignment operator, which is nothing more than the equal sign (=) symbol,<br />

such as<br />

VariableName = Value<br />

The assignment operator simply stores or assigns a value to a variable. That<br />

value can be a fixed number, a specific string, or a mathematical equation<br />

that calculates a single value. Some examples of the assignment operator are<br />

shown in Table 3-1.<br />

Table 3-1<br />

Example<br />

Age = 35<br />

Name = “Cat”<br />

A = B + 64.26<br />

Answer = “Why”<br />

Examples of Using the Assignment (=) Operator<br />

What It Does<br />

Stores the number 35 into the Age variable<br />

Stores the string “Cat” into a Name variable<br />

Adds the value stored in the B variable to the number 64.26 and<br />

stores the sum in the A variable<br />

Stores the string “Why” in the Answer variable<br />

Using Math to Manipulate Numbers<br />

Because manipulating numbers (or number crunching) is such a common<br />

task <strong>for</strong> computers, every <strong>programming</strong> language provides commands <strong>for</strong><br />

addition, subtraction, multiplication, and division. Table 3-2 lists common<br />

mathematical operations and the symbols to use.

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

Saved successfully!

Ooh no, something went wrong!