Increment and Decrement Operators in C++ - Part 1
Notes:
Increment and Decrement Operators in C++ - Part 1:
Increment and Decrement Operators:
- are used to increment or decrement a variable value by 1
++ : Increment Operator : is used to increment a variable value by 1
Post Increment : variableName ++
Pre Increment : ++ variableName
-- : Decrement Operator : is used to decrement a variable value by 1
Post Decrement: variableName --
Pre Decrement : -- variableName
Note: If we use increment or decrement operator as an independent statement then we don’t find any difference in the output where as If we use an increment or decrement operator as part of an expression or a function call then we find the difference in the output.
Example code1:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << "a= " << a << endl; // a= 10
a++; // a=a; a=a+1;
cout << "a= " << a << endl; // a= 11
int a = 10;
cout << "a= " << a << endl; // a= 10
++a; // a=a+1; a=a;
cout << "a= " << a << endl; // a= 11
int a = 10;
cout << "a= " << a << endl; // a= 10
a--; // a=a; a=a-1;
cout << "a= " << a << endl; // a= 9
int a = 10;
cout << "a= " << a << endl; // a= 10
--a; // a=a-1; a=a;
cout << "a= " << a << endl; // a= 9
return 0;
}