Home

Boolean Operations

 

Incrementing a Number

We are used to counting numbers such as 1, 2, 3, 4, etc. When counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. C++ provides a technique of transparently counting such numbers.

The simplest technique of incrementing a value consists of adding 1 to a variable. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories = Stories + 1;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3

Press any key to continue . . .

C++ provides a special operator that takes care of this operation. The operator is called the increment operator and is ++. Instead of writing Stories = Stories + 1, you can write Stories++ and you would get the same result. The above program can be re-written as follows:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

The increment++ is called a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it.

Every time the Stories++ is executed, the compiler takes the previous value of the variable and adds 1 to it and the variable holds the incremented value:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);
    Stories++;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3
Stories: 4
Stories: 5

Press any key to continue . . .

Pre and Post-Increment

When using the ++ operator, the position of the operator with regards to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Console::WriteLine(L"Stories: {0}", ++Stories);
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 3
Stories: 3

Press any key to continue . . .

When writing ++Stories, the value of the variable is incremented before being called. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the ++ operator on the right side of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 2;

    Console::WriteLine(L"Stories: {0}", Stories);
    Console::WriteLine(L"Stories: {0}", Stories++);
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

This would produce:

Stories: 2
Stories: 2
Stories: 3

Press any key to continue . . .

Decrementing: Pre and Post-Decrementing

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a variable. This operation works as if a variable called Stories had its value diminished by 1, as in

using namespace System;

int main()
{
    Byte Stories = 4;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories = Stories - 1;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

As done with the increment, C++ provides a quicker way of subtracting 1 from a variable. This is done using the decrement operator, that is --. To use the decrement operator, type –- on the left or the right side of the variable. Here is an example:

using namespace System;

int main()
{
    Byte Stories = 4;

    Console::WriteLine(L"Stories: {0}", Stories);
    Stories--;
    Console::WriteLine(L"Stories: {0}", Stories);

    Console::WriteLine();
    return 0;
}

Both versions would produce:

Stories: 4
Stories: 3

Press any key to continue . . .

Once again, the position of the operator is important. If you want to decrement the variable before calling it, position the operator on the left side of the operator. If you plan to decrement a variable after it has been accessed, position the operator on the right side of the variable.

Techniques of Incrementing and Decrementing a Variable

It is not unusual to add, or to subtract, a constant value to, or from, a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

using namespace System;

int main()
{
    double PropertyValue = 612750;
    double NewValue;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    NewValue = PropertyValue + 2420;
    Console::WriteLine(L"Property Value: {0:C}", NewValue);

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: $612,750.00
Property Value: $615,170.00

Press any key to continue . . .

The above technique requires that you use an extra variable in your application. The advantage is that each variable can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable. Sometimes in your program you will not need to keep the value of the source variable. You may want to simply modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and doesn't need an additional variable. To add a value to a variable and change the value that the variable is holding, use the assignment “=” and the addition “+” operators as follows:

using namespace System;

int main()
{
    double PropertyValue = 442824;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    PropertyValue = PropertyValue + 12850;
    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: $442,824.00
Property Value: $455,674.00

Press any key to continue . . .

C++ provides an operator that combines both + and = to allow you to increase the value held by a numeric variable. To use it, type += on the right side of the variable, followed by the value by which the variable will be incremented. Here is an example:

using namespace System;

int main()
{
    double PropertyValue = 442824;

    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);
    PropertyValue += 12850;
    Console::WriteLine(L"Property Value: {0:C}", PropertyValue);

    Console::WriteLine();
    return 0;
}

This would produce the same result as the previous program.

To decrease the value of a variable, instead of the addition operation, use the subtraction and apply the same techniques. In the above program, the variable can have its value decremented by applying the -= operator.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next