Manipulating variables

At this point we know exactly what variables are, the main types, and how to declare and initialize them, but we still can't do that much with them. We need to manipulate our variables, add them, take them away, multiply, divide, and test them.

First we will deal with how we can manipulate them and later we will look at how and why we test them.

C++ arithmetic and assignment operators

In order to manipulate variables, C++ has a range of arithmetic operators and assignment operators. Fortunately, most arithmetic and assignment operators are quite intuitive to use, and those that aren't are quite easy to explain. To get us started, let's look at a table of arithmetic operators followed by a table of assignment operators that we will regularly use throughout this book:

Arithmetic operator

Explanation

+

The addition operator can be used to add together the values of two variables or values.

-

The subtraction operator can be used to take away the value of one variable or value from another variable or value.

*

The multiplication operator can multiply the value of variables and values.

/

The division operator can divide the value of variables and values.

%

The modulo operator divides a value or variable by another value or variable to find the remainder of the operation.

And now for the assignment operators:

Assignment operators

Explanation

=

We have already seen this one. It is the assignment operator. We use it to initialize/set a variable's value.

+=

Add the value on the right-hand side to the variable on the left.

-=

Take away the value on the right-hand side from the variable on the left.

*=

Multiply by the value on the right-hand side by the variable on the left.

/=

Divide the value on the right-hand side by the variable on the left.

++

Increment operator; add 1 to a variable

--

Decrement operator; take away 1 from a variable

Note

Technically, all the above operators except for =, -- and ++ are called compound assignment operators because they comprise more than one operator.

Now that we have seen a good range of arithmetic and assignment operators we can actually see how to manipulate our variables by combining operators, variables, and values to form expressions.

Getting things done with expressions

Expressions are combinations of variables, operators, and values. Using expressions we can arrive at a result. Furthermore, as we will soon see, we can use an expression in a test. These tests can be used to decide what our code should do next. First, let's look at some simple expressions we might see in our game code:

// Player gets a new high score 
hiScore = score; 

or

// Set the score to 100 
score = 100; 

Take a look at the addition operator, used in conjunction with the assignment operator:

// Add to the score when an alien is shot 
score = aliensShot + wavesCleared; 

or

// Add 100 to whatever the score currently is 
score = score + 100; 

Notice that it is perfectly acceptable to use the same variable on both sides of an operator.

Take a look at the subtraction operator in conjunction with the assignment operator. This next code subtracts the value on the right side of the subtraction operator from the value on the left. It is usually used in conjunction with the assignment operator, for example:

// Uh oh lost a life 
lives = lives - 1; 

or

// How many aliens left at end of game 
aliensRemaining = aliensTotal - aliensDestroyed; 

This is how we might use the division operator. This next code divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:

// Make the remaining hit points lower based on swordLevel 
hitPoints = hitPoints / swordLevel; 

or

// Give player something back for recycling a block 
recycledValueOfBlock = originalValue / .9f; 

Obviously, in the previous example, the variable recycledValueOfBlock will need to be of the type float to accurately store the answer to a calculation like that.

Perhaps unsurprisingly, we could use the multiplication operator like this:

// answer is equal to 100 - of course 
answer = 10 * 10; 

or

// biggerAnswer = 1000 - of course 
biggerAnswer = 10 * 10 * 10; 

Note

As an aside, have you ever wondered how C++ got its name? C++ is an extension of the C language. Its inventor, Bjarne Stroustrup, originally called it C with classes but the name evolved. If you are interested, read the C++ story at : http://www.cplusplus.com/info/history/.

Now, let's take a look at the increment operator in action. This is a really neat way to add 1 to the value of one of our game's variables.

Take a look at this code:

// Add one to myVariable 
myVariable = myVariable + 1; 

It gives the same result as this code:

// Much neater and quicker 
myVariable ++; 

The decrement operator -- is, you guessed it, a really neat way to subtract 1 from something:

playerHealth = playerHealth -1; 

It is the same as this:

playerHealth --; 

Let's look at a few more operators in action and then we can get back to building the Timber!!! game:

someVariable = 10; 
 
// Multiply the variable by 10 and put the answer back in variable 
someVariable *= 10; 
// someVariable now equals 100 
 
// Divide someVariable by 5 put the answer back into the variable 
someVariable /= 5; 
// someVariable now equals 20 
 
// Add 3 to someVariable and put the answer back into the variable 
someVariable += 3; 
// someVariable now equals 23 
 
// Take 25 from someVariable and put the answer back into the variable 
someVariable -= 25; 
// someVariable now equals -2 

Now it's time to add some more sprites to our game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.181.87