Lesson Plan 4 C++ Operators
Lesson Plan 4 C++ Operators
C++ Operators
Pre-Requisites:
• C++ basic syntax
• Data types
• Variables
• Identifiers
• Keywords
- Subtraction Subtracts the value of right-hand operand from the value of left-hand operand
Example : num1 - num2 will give (20-30) that is -10
Example: We can use the modulus operator for checking whether a number is
odd or even; if after dividing the number by 2 we get 0 as remainder
(number%2 == 0) then it is an even number otherwise it is an odd number.
int main() {
int a;
cin >> a;
if (a % 2 == 0)
{
cout << "even no";
}
else
{
cout << "odd no";
}
return 0;
}
++ Increment This is known as the pre-increment operator and it increases the value of
operand by 1
Example : num2++ will give (30+1) that is 31
-- Decrement This is known as the pre-decrement operator and it decreases the value of
operand by 1
Example : num1-- will give (20-1) that is 19
Let us now write a simple program to implement all the operators.You may try it yourself too !
int main(){
int result;
// addition operator
result=p+q;
cout<<(result);
// subtraction operator
cout<<(p - q);
/*we can directly perform subtraction in print statement,no need to use result variable here*/
// multiplication operator
cout<<(p * q);
// division operator
cout<<(p / q);
// modulo operator
cout<<(p % q);
Output: 30
10
200
2
0
Note 1: When we divide two integers, if the dividend is not exactly divisible by the divisor, the division operator
returns only quotient and the remainder is discarded.
int main() {
int num2 = 8;
Output: 3
Note 2: In case when dividend and divisor are floating point numbers, the division operator divides dividend by
divisor till the full precision of floating point number.
Example:
#include<iostream>
int main() {
Output: 3.25
Note 3: In case either dividend or divisor is a floating point number, the division operator divides dividend
by divisor until the full precision of the floating point number.
Output: 0.301887
Note 4: floor(a) : Returns the largest integer that is smaller than or equal to a. Example floor(5.6) will return 5.
ceil(a) : Returns the smallest integer that is greater than or equal to a.
Example ceil(2.5) will return 3.
int main()
// create variables
// == operator
// != operator
// > operator
// < operator
// >= operator
// <= operator
Output: false
true
false
true
false
true
Example code:
#include<iostream>
int main(){
// && operator
int p=15,q=10,r=5;
// || operator
// ! operator
if (p == q || r == s || t == u) { // Do something }
Here,if p == q is true, then r == s and t == u are never evaluated because the expression's outcome has already
been determined and remains unaffected by the further comparisons. But, if p == q is false, then r == s is
evaluated and if (r==s) is true, then t == u is never evaluated.
Let's have a look at some of the commonly used assignment operators available in C++ with examples.
+= p += q; p = p + q;
-= p -= q; p = p - q;
*= p *= q; p = p * q;
/= p /= q; p = p / q;
%= p %= q; p = p % q;
int main(){
int p = 10;
int q;
Output: 10
20
200
Clearly x=+10 is better than x=x+10 since evaluation is direct with no intermediate steps for CPU to perform.
^ Bitwise exclusive OR
Let's look at a simple code to understand the working of these operators.
Example
#include<iostream>
using namespace std;
int main(){
// initialize p
int p = 5;
cout<<(p<<2);
// Shifting the value of p towards the left two positions
}
}
Output: 20
Note : Left shifting an integer “p” with an integer “q” denoted as ‘(p<<q)’ is equivalent to multiplying p with
2^q (2 raised to power q).
Left shifting an integer “p” with an integer “q” denoted as ‘(p<<q)’ is equivalent to multiplying p with
2^q (2 raised to power q).
Example:
Let's take p=22; which is 00010110 in Binary Form (We will learn about it in the next lecture).
If “p is left-shifted by 2” i.e p=p<<2 then p will become 01011000.
p=p*(2^2) will give, p=22*(2^2)= 22*4=88 which can be written as 01011000.
Let us now look into the next category of operators.
6. Miscellaneous Operators
The following table lists some of the miscellaneous operators that C++ supports.
int main()
{ // variable declaration
c = (a > b) ? a : b;
return 0;
5 Cast
Casting operators convert one data type to another. For example, int(4.3000) would return 4
(i.e. a float number here is converted into int)
6 &
Address-of operator & returns the memory address of a variable. For example &p; will give the
actual memory address of the variable p.
7 *
Pointer operator * points to a variable. For example *p; will point to the variable p.
Operator Meaning
+ Unary plus: optional; since numbers are by default positive.
Now that we have learnt about all types of operators, let us explore the precedence/priority of each of these wrt
each other in different scenarios.
Associativity specifies the order in which operators are evaluated by the compiler, which can be left to right
or right to left. For example, in the phrase p = q = r = 10, the assignment operator is used from right to left. It
means that the value 10 is assigned to r, then r is assigned to q, and at last, q is assigned to p. This phrase can
be parenthesized as (p = (q = (r = 10)).
Let us try a few questions on precedence and associativity to clear the air of confusion.
Example: 2
What does the following code fragment print?
cout<<(4 + 2 + "pqr");
cout<<("pqr" + 4 + 2);
Example: 3
What is the result of the following code fragment?
boolean p = false;
boolean q = false;
boolean r = true;
cout<<(p == q == r);
Ans: It prints true.
Explanation:
The equality operator is left-to-right associative, so p == q evaluates to true and this result is compared to r,
which again yields true.
Example: 4
#include<iostream>
using namespace std;
int main()
{
int p = 5, q = 10;
p += q -= p;
cout<<p<<" "<<q<<endl;
return 0;
}