Simple Unary Operators Example Program In C++

Read About C++ Operators

Unary Operators

There are two Unary Operators. They are Increment and Decrement.

Increment Unary Operator 

variable++ 

++variable;

Is Equivalent i=i+1 or i+=1

Increment Unary Operator Types

  • Post Increment i++
  • Pre Increment ++i

Decrement Unary Operator 

variable--;

--variable;

Is Equivalent i=i-1 or i-=1

Decrement Unary Operator Types

  • Post Decrement i--
  • Pre Decrement --i

Unary Operators Explanation

  • ++i: increments l and then uses its value as the value of the expression;
  • i++: uses l as the value of the expression and then increments l;
  • --i: decrements l and then uses its value as the value of the expression;
  • i--: uses l as the value of the expression and then decrements l.
  • Change their original value.

Example Program For Unary Operators

// Header Files
#include<iostream>
#include<conio.h>

//Main Function

using namespace std;

int main ()
{
	// Variable Declaration
    int a = 10;
    
    cout << "Simple Unary Operators Example Program \n";
    
    /* Increment Operators */
	cout<<"\nPost Increment = "<<a++; //Post Increment

	a = 10;
	cout<<"\nPre Increment = "<<++a; //Pre Increment

    /* Decrement Operators */
	a = 10;
	cout<<"\nPost Decrement = "<<a--; //Post Decrement i--

	a = 10;
	cout<<"\nPre Decrement = "<<--a; //Pre Decrement i--
    
	// Wait For Output Screen
	getch();

	//Main Function return Statement
	return 0;
}

Sample Output

Simple Unary Operators Example Program

Post Increment = 10
Pre Increment = 11
Post Decrement = 10
Pre Decrement = 9