Preview
Increment operator in C++ | Decrement operator in C++
Increment operator in C++
++ is known as Increment operator in C++. It adds one to the existing value of a variable of numeric or char type. It can be used in two ways:
Prefix Form of Increment operator in C++
In prefix form, increment operator ++ is placed before the variable. It immediately adds 1 one to the variable.
Example: ++n
Postfix Form of Increment operator in C++
In postfix form, increment operator is placed after the variable. It adds 1 one to the variable in next step.
Example: n++
Decrement operator in C++
— is known as decrement operator in C++. It subtracts one from the existing value of a variable of numeric or char type. It can be used in two ways:
Prefix Form of Decrement operator in C++
In prefix form, decrement operator — is placed before the variable. It immediately subtracts 1 from the variable.
Example: —n
Postfix Form of Increment operator in C++
In postfix form, decrement operator is placed after the variable. It subtracts 1 from the variable in next step.
Example: n–
//Program to demonstrate the use of increment (++) and decrement operator(–).
#include<iostream> using namespace std; int main() { int num=10; cout<<endl<<num; ++num; //a=a+1; cout<<endl<<num; num++; cout<<endl<<num; num--; cout<<“\n”<<num --num; cout<<“\n”<<num return 0; }
Output:
10 11 12 11 10
Popular Books of Computer Science