Introduction
Knowingly or unknowingly, we are all familiar what operators are ! We all have done mathematical operations at least once in our lives. Operators are basic building blocks of logic in programming language. Then what are expressions? An expression is a statement that can convey a value. Some of the most common expressions are mathematical, such as adding two values. This article will help you understand further more about them. So without any further ado
Operators
Operators are used to perform operations on Variable and values. For example :-
There are various types of operators :-
✅ Arithmetic Operators ( + , - , * , / , % , ++ , -- )
✅ Assignment Operators ( = , += , -+ , *= )
✅ Comparison Operators ( == , >= , <= )
✅ Logical Operators ( && , || , ! )
✅ Bitwise Operators ( & , | )
📌Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Remember :- Arithmetic operators cannot work with boolean. Modulo operator (%) can work on floats and doubles.
📌Assignment Operators
These operators are used to add, sub, multiply, etc. the assigned value to the variable.
For example:
The addition assignment operator (+=) adds a value to a variable!
📌Comparison operator
Comparison operators are used to compare two values. Various types of comparison operators are as follows:-
In the below examples, I used greater than comparison operator '>' and it returns "True" value because 5 is greater than 3.
📌Logical Operators
Logical operators are used to determine the logic between two variables.
In the below example, I used Logical AND operation and I got "True" in return because 5 is greater than 3 and 5 is less than 10. 👇🏻
📌Bitwise Operator
Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). This topic is so big that I have to write a separate article on this, if your in hurry please google it or you can read my article when I'll post it. ( I recommend you to google as topic should be covered quickly and should not be postponed😊)
Precedence of operators
The operators are applied and evaluated based on precedence. Like for example, "+" has a less precedence as compared to say "*" and "/". So Multiplication and division says to addition and subtraction that we are bigger than you so we will operate first. For example 👇🏻
In the above example, it went division first and then subtraction and we get 1 as answer.
Associativity of operators
Multiplication and division has same precedence so how do we evaluate when both come in one equation? Here the law of associativity comes into picture. Law of associativity acts from left to right. Let us see this with an example👇🏻
But you may ask, Vinayak how to do we know that which has greater associativity? (If they have same precedence) Don't worry my friend here you go⬇️
I got this from code with Harry's YouTube channel so thanks to him for making this. So, coming back to our topic, first check the precedence and then associativity. So, if there is addition and multiplication then multiplication will operate first and then addition. But if there are division and multiplication then check the associativity column and accordingly get to your answer. Remember, associativity law operate from left to right or right to left so operate in that order accordingly.
Associativity tells us direction of execution. It can be either from left to right or right to left.
'*' and '/' then it is L to R
'++' and '=' then it is R to L
Increment and decrement operators
✅Increment Operators are i++ and ++i
✅Decrement Operators are i-- and --i
Lets understand this for increment operators first and same is for decrement operators only difference is that in increment we will add and in decrement we will subtract.
So when we use " i ++ " the value will be first printed and then it will be incremented by 1. Please remember here that in place of i you can take any variable name. Have a look at the example given below⬇️
So, first we get 3 and then new value is printed as 4. Now, let us understand what "++i" does? It first increments and then it prints! Have a look below ⬇️
Here in the above example of ++i, it is first incremented and then the new value is printed!
Same goes for i-- and --i, only difference is that we subtract here.
THANKS FOR READING MY ARTICLE📄 HOPE THAT IT ADDS VALUE⤴️
FOLLOW ME ON HASHNODE VINAYAK PANCHAL ✅