Prefixing And Postfixing In Increment And Decrement Operators
- Both increment (++) and decrements (--) 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)
- In postfix the increment or decrement operator is written after the variable's name (a++ or a--).
- Following example given below:-
#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;
}
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;
}
No comments:
Post a Comment