used basic operators like +,-,*,/ and =. Let us dig deeper into some more operators.
NOTE: This post is probably too long, but is easy and important nonetheless. And don't be scared if you don't get it in one go. Ask questions in the comments section. :)
ARITHMETIC OPERATORS are those operators that perform arithmetic operations, namely addition, subtraction, multiplication, division etc. The modulus operator is used to return the remainder and is represented by % symbol. All the above operators are BINARY OPERATORS, meaning that they act upon two "OPERANDS". The operands can both be variables (a%b), a constant and a variable (3.14*a) or both constants (23-15). UNARY OPERATORS are operators that act on single operand. C++ provides two unary arithmetic operators, unary + and unary -. A number with no unary operator before it is assumed to be positive. So, the following statements are equivalent:
int a=5;
int a=+5;
The unary - sign negates the value of operand, or changes its sign. See the examples below for clarity:
int a=10;
int b=-a; //assigns -10 to variable b
int c=-b; //changes the sign again i.e. c now stores 10
INCREMENT/DECREMENT OPERATORS add or subtract one from the operand. They are unary operators and represented by ++ and -- respectively. They can be used before (pre increment/decrement) or after (post increment/decrement) the variable name.
int i=5;
i++; //value of i becomes 6
++i; //value of i becomes 7
i--; //value of i becomes 6
--i; //value of i becomes 5
You might wonder, what is the difference between the two? Well, these operators can be used as it is, or their value can be assigned to another variable. This is where the difference comes.
Case 1: Case 2:
int i=2; int i=2;
int j=++i; int j=i++;
Both the above cases are perfectly correct. After execution of both cases, the value of i becomes 3. But what about j? The first case uses pre increment, which has a FIRST CHANGE, THEN USE rule. That means, it first changes the value of i and then strores the changed value in j. So j also has a value of 3 in first case.
In the second case, there is post increment, which has a FIRST USE, THEN CHANGE rule. That means, that it first assigns the existing value of i to j and then increments it. So value of j is 2 in this case.
At some places, it does not matter whether you use pre or post increment/decrement. In such cases, Pre Increment is generally suggested as it is faster.
RELATIONAL OPERATORS are the operators that tell about the relation between two operands. In C++, following relational operators are present:
< (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), != (not equal to)
Let us consider a=10 and b=5.
So, a < b will result in FALSE, a > b will be TRUE, a<=b will be FALSE, a>=b will be TRUE, a==b will be FALSE, a!=b will be TRUE. Also, a==10 will be TRUE, but a==7 will be FALSE. These operators are an essential part of SELECTION and ITERATION programming construct and used to check the conditions upon which the execution of program depends.
CONDITIONAL OPERATOR is a TERNARY OPERATOR, meaning that it acts upon THREE operands It's general form (or syntax) is:
expression1?expression2:expression3;
That is, if the value of expression1 is true, then expression2 is executed, otherwise expression3 is executed.
For instance, let a variable 'a' of integer type has a value 100. Then
(a==100)?b=10:b=20;
will assign a value of 10 to the variable 'b'. However,
(a==200)?b=1:b=2;
will assign the value of 2 to the variable 'b'.
Some other methods to use this operator are:
(a==10)?cout<<"a is 10":cout<<"a is not 10"; //Outputs one of the given lines, depending on the value of 'a'
b= (a==10)?0:1; //assigns 0 to 'b' if 'a' is equal to 10, otherwise assigns 1 to 'b'
ASSIGNMENT OPERATORS are used to assign the value of right hand side to the variable on the left hand side of the operator. '=' is the most commonly used assignment operator. Undestand clearly that '=' assigns the value, while '==' compares two values. So, if a=5,b=10, then a=b will assign the value 5 to variable 'a' and a==b will result in FALSE.
LOGICAL OPERATORS establish a logical relationship between one or more operands. C++ provides logical AND (&&), OR (||) and NOT (!) operators. AND results in TRUE if both of its operands are TRUE, otherwise it is false. OR results in TRUE if ATLEAST ONE of the operands is TRUE. NOT simply NEGATES/CHANGES the value of operand i.e. TRUE becomes FALSE and vice-versa.
NOTE: Any NON-ZERO value is considered as TRUE, while ZERO is considered to be FALSE in C++.
So, if a=4 and b=0, a&&b will be FALSE, a||b will be TRUE, !a will be FALSE, !b will be TRUE. Also, only a will be TRUE and only b will be FALSE. Also, (a+b)>(a-b) will be TRUE and !(a-4) will be TRUE.
COMMA OPERATOR is used to separate variables while declaring them. Ex:
int a,b,c;
PRECEDENCE: If you use more than one operand in a single statement, then which one should be applied first? Just like we have BODMAS in Mathematics, we have precedence in C++, which decides which operation should be performed first. Ex: multiplication has higher precedence than addition. Parenthesis (represented by '(' and ')' ) are used to change that order. For instance, if a=10 and b=5,
a+b*10
will be calculated as:
10+ 5*10
=10+ 50
=60
If we write:
(a+b)*10
then it will be calculated as:
(10+5)*10
=15*10
=150
If two operators have the same precedence, then compiler decides whether they are applied from right to left or left to right. This is called ASSOCIATIVITY OF OPERATORS.
The whole list about precedence and associativity of operators can be found here:
http://web.ics.purdue.edu/~cs240/misc/operators.html
for a more detailed list, you can visit the wikipedia page:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
For more facts on operators, precedence and associativity, you can visit this page:
http://www.geeksforgeeks.org/c-operator-precedence-associativity/
There are other operators too, like the bitwise operator and the scope resolution operator, but these are more than sufficient for now.
Was it too difficult? Did i miss something? Should i write smaller posts? Feel free to comment. ;)
No comments:
Post a Comment