Operators in C++ Part 2
Assignment Operators
In C++, assignment operators are like magic arrows that help you give values to things. Imagine you have a box and want to put some toys inside it. The assignment operator is like the hand that places the toys in the box.
Assignment operators are used to assign values to variables.
Here are the basic assignment operators:
1. Equal to (=
):
This is the most basic assignment operator, it helps us to assign values to different variables.
int candies;
candies = 5; // Now, the variable 'candies' has 5 inside it.
2. Add and Assign (+=
):
It helps us to increment the value and then re-assign it to the variable.
int toys = 3;
toys += 2; // Now, the variable 'toys' has 5 inside it (3 + 2).
toys += 2 is equivalent to toys = toys + 2;
3. Subtract and Assign (-=
):
It helps us to decrement the value and then re-assign it to the variable.
int toys = 7;
toys -= 3; // Now, the variable 'toys' has 4 inside it (7 - 3).
toys -= 3 is equivalent to toys = toys - 3;
4. Multiply and Assign (*=
):
It helps us to multiply the value and re-assign it to the variable.
int toys = 4;
toys *= 3; // Now, the variable 'toys' has 12 inside it (4 * 3).
toys *= 3 is equivalent to toys = toys * 3;
5. Divide and Assign (/=
):
It enables us to divide the value and then re-assign it to the variable.
int toys = 10;
toys /= 2; // Now, the variable 'toys' has 5 inside it (10 / 2).
toys /= 2 is equivalent to toys = toys/2;
Quiz
What is the output of the following code?
#include <iostream>
using namespace std;
int main(){
int x = 15;
x -= 5
x *= 3
x /= 3
x %= 3
cout << x << endl;
return 0;
}
Relational Operators
Relational operators are like little detectives that help us compare things and see how they relate to each other. We use them when we want to find out if something is greater than, less than, equal to, or not equal to something else.
Using relational operators either results True or False.
Here are the basic relational operators:
1. Greater Than (>):
Example: If you have 5 apples and your friend has 3 apples, we can say that 5 is greater than 3. So, 5 > 3.
#include<iostream>
using namespace std;
int main(){
int x = 5;
int y = 3;
cout << ( x > y ) << endl;
return 0;
}
Output :
1
Note: Since x is greater than y, hence the above operation results in True and cout statement converts the boolean True
as integer 1
. If it would have been False
then 0
would have been printed.
2. Less Than (<):
Example: If you have 2 cookies and your sister has 4 cookies, we can say that 2 is less than 4. So, 2 < 4.
#include<iostream>
using namespace std;
int main(){
int x = 5;
int y = 3;
cout << ( x < y ) << endl;
return 0;
}
Output :
0
3. Equal To (==):
Example: If you have 6 candies and your cousin also has 6 candies, we can say that the number of candies you have is equal to the number your cousin has. So, 6 == 6.
#include<iostream>
using namespace std;
int main(){
int x = 5;
int y = 5;
cout << ( x == y ) << endl;
return 0;
}
Output:
1
4. Not Equal To (!=):
Example: If you have 8 stickers, and your classmate has 8 pencils, we can say that the number of stickers you have is not equal to the number of pencils your classmate has. So, 8 != 8 (because stickers are not equal to pencils).
#include<iostream>
using namespace std;
int main(){
int x = 10;
int y = 5;
cout << ( x != y ) << endl;
return 0;
}
Output :
1
Quiz
What is the output of the following code?
#include <iostream>
using namespace std;
int main(){
int x = 10;
int y = 15;
int z = 10;
y -= z;
y *= 2;
cout << x == y << " ";
cout << y == z;
return 0;
}
Special Operator
sizeof() Operator
So far, we have used different types of data in our programs. All these data are stored in computer memory.
If we want, we can find how much storage each data is occupying in the memory. For this, we can use the sizeof()
operator
sizeof(variable)
The sizeof()
operator finds the size of values and variables. Let's see an example:
#include <iostream>
using namespace std;
int main() {
int age;
double salary;
char alphabet;
// sizeof gives the size in bytes
cout << "char: " << sizeof(char) << " byte" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Output :
char: 1 byte
int: 4 bytes
double: 8 bytes
Here, all the sizes are in bytes. For example, the size of int
is 4 bytes.
We have learnt all the important types of operators and will learn some more advanced operators in further lectures.