-->

WAP to understand unary operator(prefix and postfix).

//program to understand prefix and postfix operator
#include <iostream>//header file
using namespace std;//to handle all standard characters/functions,reserved words  used in program. we do not use in turboc++ compiler.

int main()          //main function
{
    int a=4,b;
    cout << "++a="<<++a << endl;//gives 5. It means first increase the value then use that. Its increased part (a=a+1) is invisible.
    a=4;
    cout<<"a++="<<a++;//gives 4. It means first use the value then increase the value. Its increased part is excuted after this statement and is not shown here.
                     // ++ is called increment operator. It can be prefix (++a)or postfix(a++).
    b=5;
   cout << "--a="<<--a << endl;//gives 4. It means first decrease the value then use that. Its decreased part (a=a-1) is invisible.
                    //-- is called decrement operator. It can be prefix(--a) and postfix(a--).
    b=5;
    cout<<"a--="<<a--;//gives 5. It means first use the value then decrease the value. Its decreased part is executed after this statement and is not shown here.
    return 0;
}

No comments:

Post a Comment