Operators in C++
Operators are symbols in C++ used to perform operations on variables and values.
They are the building blocks of C++ programming, enabling various computations
and decision-making processes.
1. Types of Operators in C++
1.1 Arithmetic Operators
Used to perform basic mathematical operations.
Operato Description Exampl Result
r e
+ Addition a+b Sum
- Subtraction a-b Difference
* Multiplication a*b Product
/ Division a/b Quotient
% Modulus (remainder) a % b Remainder
Example:
int a = 10, b = 3;
cout << "Sum: " << a + b << endl; // Output: 13
cout << "Remainder: " << a % b << endl; // Output: 1
1.2 Relational (Comparison) Operators
Used to compare two values, resulting in a boolean value (true or false).
Operato Description Example Result
r
== Equal to a == b true/false
!= Not equal to a != b true/false
< Less than a<b true/false
, > Greater than a>b true/false
<= Less than or equal to a <= b true/false
>= Greater than or equal to a >= b true/false
Example:
int a = 5, b = 10;
cout << (a > b); // Output: 0 (false)
cout << (a <= b); // Output: 1 (true)
1.3 Logical Operators
Used to combine or invert boolean expressions.
Operator Description Example Result
&& Logical a && b true if both are
AND true
` ` Logical OR
! Logical NOT !a true if a is false
Example:
bool a = true, b = false;
cout << (a && b); // Output: 0 (false)
cout << (a || b); // Output: 1 (true)
cout << (!a); // Output: 0 (false)
1.4 Assignment Operators
Used to assign values to variables. Some operators combine assignment with
arithmetic operations.
Operator Description Exampl Equivalent To
e
= Assignment a=b Assign b to a
+= Add and assign a += b a=a+b
-= Subtract and assign a -= b a=a-b
Operators are symbols in C++ used to perform operations on variables and values.
They are the building blocks of C++ programming, enabling various computations
and decision-making processes.
1. Types of Operators in C++
1.1 Arithmetic Operators
Used to perform basic mathematical operations.
Operato Description Exampl Result
r e
+ Addition a+b Sum
- Subtraction a-b Difference
* Multiplication a*b Product
/ Division a/b Quotient
% Modulus (remainder) a % b Remainder
Example:
int a = 10, b = 3;
cout << "Sum: " << a + b << endl; // Output: 13
cout << "Remainder: " << a % b << endl; // Output: 1
1.2 Relational (Comparison) Operators
Used to compare two values, resulting in a boolean value (true or false).
Operato Description Example Result
r
== Equal to a == b true/false
!= Not equal to a != b true/false
< Less than a<b true/false
, > Greater than a>b true/false
<= Less than or equal to a <= b true/false
>= Greater than or equal to a >= b true/false
Example:
int a = 5, b = 10;
cout << (a > b); // Output: 0 (false)
cout << (a <= b); // Output: 1 (true)
1.3 Logical Operators
Used to combine or invert boolean expressions.
Operator Description Example Result
&& Logical a && b true if both are
AND true
` ` Logical OR
! Logical NOT !a true if a is false
Example:
bool a = true, b = false;
cout << (a && b); // Output: 0 (false)
cout << (a || b); // Output: 1 (true)
cout << (!a); // Output: 0 (false)
1.4 Assignment Operators
Used to assign values to variables. Some operators combine assignment with
arithmetic operations.
Operator Description Exampl Equivalent To
e
= Assignment a=b Assign b to a
+= Add and assign a += b a=a+b
-= Subtract and assign a -= b a=a-b