4 - C++ Operators PDF
4 - C++ Operators PDF
+ a+b Addition
- a–b Subtraction
* a*b Multiplication
/ a/b Division
operator description
== Equal to
!= Not equal to (7 == 5) // evaluates to false
< Less than (5 > 4) // evaluates to true
(3 != 2) // evaluates to true
> Greater than (6 >= 6) // evaluates to true
(5 < 5) // evaluates to false
<= Less than or equal to
>= Greater than or equal to
Let a=2, b=3 and c=6
(b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
|| OPERATOR (or)
a b a || b
true true true
true false true
false true true
false false false
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false )
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )
INCREMENT
Prefix increment
In this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is
used.
int a=1,b;
b=++a;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 2 and b is 2
Postfix increment
In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used.
int a=1,b;
b=a++;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 2 and b is 1
DECREMENT
Prefix decrement
In this method, the operator precedes the operand (e.g., --a). The value of operand will be altered before it is
used.
int a=1,b;
b=--a;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 0 and b is 0
Postfix decrement
In this method, the operator follows the operand (e.g., a--). The value operand will be altered after it is used.
int a=1,b;
b=a--;
cout<<"a is "<<a<<" and b is "<<b<<endl;
Output:
a is 0 and b is 1
What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
Output:
int x, y;
x = 5; 7 42
y = ++x * ++x; 7 35
cout << x <<"\t"<< y<<endl;
x = 5;
y = x++ * ++x;
cout << x <<"\t"<< y<<endl;
}
Conditional Operator
Syntax:
exp1 ? exp2 : exp3
Working of the ? Operator:
§ exp1 is evaluated first
• if exp1 is true(nonzero) then
- exp2 is evaluated and its value becomes the value of the expression
• If exp1 is false(zero) then
- exp3 is evaluated and its value becomes the value of the expression
Ex: Ex:
m=2; m=2;
n=3; n=3;
r=(m>n) ? m : n; r=(m<n) ? m : n;
Value of r will be 3 Value of r will be 2
Conditional ternary operator ( ?: )
The conditional operator evaluates an expression, returning one value if that expression
evaluates to true, and a different one if the expression evaluates as false. Its syntax is:
Why?? Because if you would have taken 8 bits then answer would have been different !!
And if you would have taken 16 bits or 32 bits then answer would have been different !!
Lets say
int a=10, b=10, c;
c=(!a==b);
cout<<c;
It is either a literal |
Always it is a
a variable identifier |
variable identifier.
an expression.
Literal: ex. i = 1;
Variable identifier: ex. start = i;
Expression: ex. sum = first + second;
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;
It is an arithmetic
operator.
1) Unary Operators: Those operators that require only single operand to act upon are known as
unary operators.
For Example: unary minus (-), increment (++) and decrement (--) operators
2) Binary Operators: Those operators that require two operands to act upon are called binary
operators.
Binary operators are classified into :
Arithmetic operators, Relational Operators, Logical Operators, Assignment Operators,
Bitwise Operators
3) Ternary Operators: These operators requires three operands to act upon. For Example:-
Conditional operator(?:).
Unary Operators
SrNo Operators Symbols
1 Unary plus +
2 Unary minus -
3 Increment operator ++
4 Decrement operato --
5 Address of Operator &
6 Size of Operator sizeof()
7 Dereferencing *
Operator
8 Logical NOT !
9 Bitwise NOT/ ~
Bitwise Negation/
One's Compliment
Unary minus
The minus operator changes the sign of its argument. A
positive number becomes negative, and a negative number
becomes positive.
int a = 10;
int b = -a; // b = -10
NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
If x is true, then !x is false
If x is false, then !x is true
Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a
variable.
#include <iostream>
using namespace std;
int main()
{
float n = 0;
cout << "size of n: " << sizeof(n);
return 0;
}
OUTPUT: size of n: 4
Precedence Operator Meaning
1 – (unary) Returns the negative of its argument
2 ++ (unary) Increment
2 — (unary) Decrement
3 * (binary) Multiplication
3 / (binary) Division
3 % (binary) Modulo
4 + (binary) Addition
4 – (binary) Subtraction
5 =, *=,%=,+=,-= Assignment types
Precedence Example
x = 5 + 7 % 2; gives x=6
x = 5 + (7 % 2); gives x = 6
x = (5 + 7) % 2; gives x = 0