Monday, April 16, 2012

Back From the Dead

Hello to all my little bots who thrive within my blog. I've been sick for the past couple of weeks so my blog posts and studies had been put on hold. One word of warning if you ever wish to go to Asia, if you catch a cold it will knock the stuffing out of you. Not sure what it is about here, but every time I get sick it's always for at least a week. More than likely it'll be for a couple of weeks. The darn bug just doesn't want to die. Never had that problem back in the states, but here we have super colds.

Well, enough whining back to C++.

Last post I talked about variables and how to assign them a value. Now we are going to mess around with assigning values to the variables.

Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned int hitPoints = 50;
    cout << "Hit Points: " << hitPoints << endl;
   
    //altering the value
    hitPoints = hitPoints + 100;
    cout << "Hit Points: " << hitPoints << endl;
   
    //shorter way to write the above line of code
    hitPoints += 100;
    cout << "Hit Points: " << hitPoints << endl;
   
    //Increment and Decrement Operators
   
    short extraLives = 1;
    cout << "Extra Lives: " << extraLives << endl;
   
    ++extraLives;
    cout << "Extra Lives: " << extraLives << endl;
   
    extraLives++;
    cout << "Extra Lives: " << extraLives << endl;
   
    extraLives--;
    cout << "Extra Lives: " << extraLives << endl;
   
    extraLives = 1;
    short bonus = ++extraLives * 5;
    cout << "Extra Lives, Bonus = " << extraLives << ", " << bonus << endl;
   
    extraLives = 1;
    bonus = extraLives++ * 5;
    cout << "Extra Lives, bonus = " << extraLives << ", " << bonus << endl;
     

   
    system("PAUSE");
    return EXIT_SUCCESS;
}


That little piece of code will display this when ran:

Hit Points: 50
Hit Points: 150
Hit Points: 250
Extra Lives: 1
Extra Lives: 2
Extra Lives: 3
Extra Lives: 2
Extra Lives, Bonus = 2, 10
Extra Lives, Bonus = 2, 5


You can alter the value of any variable you entered by simply writing it again and giving it a new value. No need to declare it as a short int or anything. It already knows what it is.

For example: I gave my ficticious character 50 hit points with this:
short hitPoints = 50

I then altered the value with this:
hitPoints = hitPoints + 100
I told it to take the value of hitPoints and add a hundred to it. So now hitPoints is 150.

There is an even simplier way of doing that by using a combined assignment operator. In this case I used the addition operator.
hitPoints += 100

That tells the compiler to add 100 to hitPoints.
You can do this with subtraction and the rest. Here's the list:

+=    adds              example: x += 20;        same as: x = x + 20;
-=    subtracts        example: x -= 20;         same as: x = x - 20;
*=  multiplies       example: x *= 20;         same as: x = x * 20;
/=   divides           example: x /= 20;          same as: x = x / 20;
%= modulus        example: x %= 20;        same as x = x % 20;

Next I used an Increment and decrement operator.

++hitPoints;
hitPoints++;

--hitPoints;
hitPoints--;

Those add one to the variable. In my code I declared the variable extralives and gave it a value of one. Then I added one to that by using ++hitPoints. So now extraLives had a value of 2. I wrote it two different ways.

++extraLives;  this will add one to extraLives before doing anything else.
extraLives++ this will add it after.

I know it doesn't sound like much of a difference, but if you look at this:

    extraLives = 1;
    short bonus = ++extraLives * 5;
    cout << "Extra Lives, Bonus = " << extraLives << ", " << bonus << endl;
   
    extraLives = 1;
    bonus = extraLives++ * 5;
    cout << "Extra Lives, bonus = " << extraLives << ", " << bonus << endl;


The first bit displays:

Extra Lives, Bonus = 2, 10


and the second one displays:


Extra Lives, Bonus = 2, 5


The reason for this is that the first bit added the ++ and then multiplied extraLives by 5.

The second line added it after it multiplied it by 5. So that extraLives was 2 in both cases, but the bonus was different.

That'll do it for today. Still feeling a bit sick, but wanted to get back to my blog and coding. I hope my bots and one fan are happy again :D

As always:
Same rat channel.
Same rat time.
 

No comments:

Post a Comment