Sunday, November 22, 2009

Increment And Decrement Operator : prefix and postfix


Increment And Decrement Operator in C++

In C++ Programming Language, increasing a value by 1 is called incrementing and decreasing value by 1 is called decrementing. As 1 is the most common value used to add, subtract and to reassign into variable. Although we have short-hand assingnment operator but special operator are provided in c++ programming to increase or decrease value by 1. The increment operator(++) increases the variable's value by 1 and the decrement operator(--) decreases the value by 1.

A C++ Program example that demonstrate the use increment and decrement operator by comparing it with short hand assignment operator.


#include <iostream>

int main ()
{

    using std::cout;
    using std::endl;

    int b = 4, c = 7;

    cout << "The value of b is : " << b << endl;
    b = b + 1;
    cout << "The value of b is : " << b << endl;
    b += 1;
    cout << "The value of b is : " << b << endl;
    b++;
    cout << "The value of b is : " << b << endl;

    cout << endl;

    cout << "The value of c is : " << c << endl;
    c = c - 1;
    cout << "The value of c is : " << c << endl;
    c -= 1;
    cout << "The value of c is : " << c << endl;
    c--;
    cout << "The value of c is : " << c << endl;

    return 0;
}


Prefixing And Postfixing In Increment And Decrement Operator.

Both increment (++) and decrement (--) operator come in two varieties : prefix and postfix. In prefix the increment or decrement operator is written before the variable's name (++a or --a) and in postfix the increment or decrement operator is written after the variable's name (a++ or a--).

In simple statement it doesn't matter what you choose. But it differs in complex statement in which you increment or decrement value and also assign it to a variable in single statement. The prefix operator is evaluated before the assignment. The postfix opertor is evaluated after the assignment. Follwing example makes it clear.

A C++ Program example that demonstrate the similarities and difference between prefix and postfix operator.


#include <iostream>

int main ()
{

    using std::cout;
    using std::endl;

    int a = 10;

    a++;
    cout << "The value of a is : " << a << endl;
    ++a;
    cout << "The value of a is : " << a << endl;
    cout << "The value of a is : " << a++ << endl;
    cout << "The value of a is : " << ++a << endl;

    cout << endl;

    int b = 3;
    a = b++;
    cout << "The value of a is : " << a << endl;
    cout << "The value of b is : " << b << endl;
    a = ++b;
    cout << "The value of a is : " << a << endl;
    cout << "The value of b is : " << b << endl;

    return 0;
}


Another C++ Program example that demonstrate the use of postfix and prefix operator.


#include <iostream>

int main ()
{

    using namespace std;

    int age = 18;

    cout << "I was " << age++ << " years old." << endl;
    cout << "Now I am " << age << " years old." << endl;
    cout << "One year passes...." << endl;
    cout << "I am " << ++age << " years old." << endl;

    return 0;
}

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

2 comments: