C++ Operators

Operators Overview

Once we know of the existence of variables and constants, we can begin to operate with them. For that purpose, C++ integrates operators. Unlike other languages whose operators are main keywords, operators in C++ are mostly made of signs that are not part of the alphabet but are available in all keyboards. This makes C++ code shorter and more international, since it relies less on English words, but requires a little of learning effort in the beginning.
You do not have to memorize all the content of this page. Most details are only provided to serve as a later reference in case you need it.

Arithmetic Operator

They are five arithmetic operators in C++.

  • +         Addition or unary plus
  • -          Subtraction or unary minus
  • *          Multiplication
  • /          Division
  • %        Modulo operator

These operators can operate on any arithmetic operations in C++.

Simple Arithmetic Operators Example Program In C++

Relational Operators

  • It Compares two operands and depending on their relationship.

Syntax

Op1 Operator Op2

Example :

a > b

There are six relational operators. They are,

  • <  less than
  • >  greater than
  • <= less than or equal to
  • >= greater than or equal to
  • == equal to
  • != not equal to

Simple Relational Operator Example Program In C++

Logical Operators

  • AND, OR operators are used when we want to use two or more Conditions.

Types Of Logical Operators

  • && Logical AND
  • || Logical OR
  • !  Logical NOT

Logical And (&&) Operator 

Logical And Operator Definition

If both the operations are successful, then the condition becomes true.

Logical And Operator Syntax

expr1 && expr2

Logical And Operator Syntax Example

if( (a>10) && (a<20) )
  printf(?A is in-between of 10 and 20?);

Simple Logical Operators Example Program In C++

Assignment Operators

They are used to assign the result of an expression to a variable.

Syntax

identifier = Expression

There are five assignment operators. They are,

a=a+1 a+=1

a=a- 1 a-= 1

a=a*2 a*=2

a=a/2 a/= 2

a=a%2 a%=2

Advantages:

  • Left-hand side operator need not repeat.
  • Easy to Read
  • More Efficient

Simple Assignment Operators Example Program In C++

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.

Simple Unary Operators Example Program In C++

Conditional or Ternary operator

Definition

Check condition if true, it returns first variable value otherwise return second values. sometimes it replaces if..else statement

Syntax

Condition? Expression1: Expression2

Example

(a>10) ? b : c

Explanation For Conditional or Ternary operator 

Given that

a, b, c

are expressions;

the expression

(a>10) ? b : c

has as its value b if a is nonzero, and c otherwise. Only expression b or c is evaluated.

Expressions b and c must be of the same data type. If they are not but are both arithmetic data types, the usual arithmetic conversions are applied to make their types the same. It is also called ternary operators.

Simple Conditional or Ternary Operators Example Program In C++

The Comma Operator

  • The Comma operator can be used to link the related expressions together.

Example For Comma Operator

Comma Operator In for Loops
for(i=0,j=1;i>10:i++,j++)

Comma Operator In while Loops

While(c<10,c--)

Simple Comma Operators Example Program In C++

Scope Resolution Operator

The scope resolution operator is used for the Unary scope operator if a namespace scope  (or) Global Scope

Scope Resolution Operator Syntax

:: identifier   // for Global Scope
class-name :: identifier  // for Class Scope
namespace :: identifier   // for Namespace Scope

//simple syntax
:: global variable name

For more information on Scope Resolution Operator, check this link

Detailed Explanation of Scope Resolution Operator

 

Simple Scope Resolution Operator Example Program In C++