An Expression is composed one more or more operand and operations to be applied on them.
In C++ Operand can by anything like an integer, a float, or a array and the operations applied to the operands are represented by Operators.
We can classify the Operators in three Categories -
- Unary Operators.
- Binary Operators.
- Ternary Operator. ( will be discussed at last )
#include<iostream> using namespace std; int main() { int a = 10; // Initialization of variable 'a' int *b = &a; // Storing address using unary operators '*' and '&' in b return 0; }
2. Binary Operators - As I already discussed, the operators that act on two operands are binary operators.
There are many such operators exist in C++ language. Let's see the table.
We will discuss each of these operators one by one in this Order.
- Arithmetic Operators.
- Assignment Operators.
- Relational Operators.
- Logical Operators.
- Unary Operators.
- Bitwise Operators.
- Ternary Operators.
- Addition (+) operators - We use it to add two numbers, e.g. (10 + 20) gives us 30 after addition.
- Subtractions (-) operator - We use it to subtract numbers, e.g. (10-20) gives us -10.
- Multiplication (*) operator - We use it to product/multiply two numbers. e.g. (10*20) gives us 200.
- Division (/) operator - We use it divide numerator by denominator, e.g. (10/20) gives us 0.5.
- Modulus or Remainder Operator - We use to find the remainder after successively dividing the numerator by the denominator, e.g. (7%4) gives us 3.
#include<iostream> using namespace std; int main() { int a = 7; cout << a + 3 << endl; // Addition operator cout << a - 3 << endl; // Subtraction operator cout << a * 3 << endl; // Multiplication operator cout << a / 3 << endl; // Division operator cout << a % 3 << endl; // Modulus operator return 0; }
#include<iostream> using namespace std; int main() { int a = 10; a += 10; // Equivalent to a = a + 10; a -= 10 // Equivalent to a = a - 10; a *= 10; // Equivalent to a = a * 10; a /= 10; // Equivalent to a = a / 10; a %= 10 // Equivalent to a = a % 10; return 0; }
The above program is sufficient for understanding these operators.
3. Relational Operators - These Operators describe what is the relation between 2 operands and on the basis of the relation, return some boolean true/false, let's see them one by one.
- Greater than (>) operator - If the left operand is greater than right operand, then it evaluates to true.
- Greater than or equal to (>=) operator - If the left operand is greater than or equal to right operand, then it evaluates to true.
- is equal to (==) - It is different from assignment operator (=), syntactically as well as operationally, If the left operand is equal to the right operand then it evaluates to true.
- less than (<) - If the left operand is less than the right operand then it evaluates to true.
- Less than or equal to (<=) - If the left operand is less than or equal to the right operand then it evaluates to true.
- Not equal to (!=) - If the left operand is not equal to right operand then it evaluates to true.
- Logical and (&&) - If the left and the right expression both evaluate as true, the && operator will evaluate as true. e.g. if(1 < 3 && 3 < 5) is a tautology(the statement which is always true).
- Logical OR (| |) - If any one of the expression, either on left or on right side evaluates to true, the OR operator will evaluate to true, e.g. if(1 > 3 || 3 < 4), here even if left expression evaluates to false, but the right expression is true, So, the final result is true.
- Logical Not (!) - Actually, it is a unary operator, it makes the true condition false, and false condition true. like if(!(1<3)) will evaluates to false even if 1 < 3 is true.
- Increment operator (++) - It is a upgraded version of assignment operator, but only for a specific task, as in assignment operator we write like, something += something else, but, if the value of something else is "1" then we can replace the whole expression with something++ (post increment) or ++something(pre increment) according to situation. We will cover post increment and pre-increment with some good example in some other slide.
- Decrement Operator (--) - We can relate this one with the same example but with something -= something else. and if something else is "1" we can write it in two ways, try to guess them.
#include<iostream> using namespace std; int main() { int a = 10; a++; // Increased value by one cout << a << endl; // print 11 a--; // decreased value by one cout << a << endl; // prints 10 return 0; }
- Bitwise AND (&) - Like And operator we have a Bitwise AND too, but we use single ampersand here. It is used to compare the set bits of two numbers (set bits are bits which is turned on i.e. bits with value 1), Bitwise AND is used in bitmasking, one simple use of of Bitwise AND is to find if the number is odd or not, to do so we can simply check the first bit of the given number as we know the weightage of the first bit is 2^0 which is just 1, every other bit of a number will evaluate to a even number of first bit is 0 but if first bit is one the number is guaranteed to be odd. I will show it's syntax while covering the decision making statements in C++.
- Bitwise OR (|) - It syntax is a single bar. It is also used in bitmasking. It like OR but the expression here are bits, 1 means true and 0 means false. if any true it's true.
- Bitwise XOR(^) - The name looks similar to OR but with extra X, the idea of XOR is if only one of the bit is 1 then it gives true otherwise false, which simply means, both bits should not be same. Simple application could be finding a number occuring odd number of times in an array.
- Bitwise LeftShift(<<) - This operator is to shift the bits of number(say N) towards left i.e. from Low significant bit to high significant bit. Which means if we left shift the bits by i numbers than the N = N*2^i and syntax will look like N = (N<<i);
- Bitwise RightShift(>>) - This operator is used to shift the bits of a number(say N) towards right, i.e. from High significant bit towards Low significant bit. Which means if we right shift the bits by i numbers than the N = N / 2^i and syntax will look like N = (N>>i);
- Bitwise Not (~) - It is a bitwise equivalent of not operator and also a unary operator, the simple application could be to turn off(making 0) the ith bit of a number (say N). Syntax looks like N &= ~(1<<i); I will cover all these topics in a separate slide.
#include<iostream> using namespace std; int main() { int a = 7; int b = 10; cout << (a & b) << endl; // Bitwise AND cout << (a | b) << endl; // Bitwise OR cout << (a ^ b) << endl; // Bitwise XOR cout << (a << b) << endl; // Bitwise left shift print a*2^10 cout << (a >> b) << endl; // Bitwise rigth shift print 0 cout << (~a) << endl; // Bitwise Not is a unary opeator too. return 0; }
// Program for printing largest of two numbers? #include<iostream> using namespace std; int main() { int a = 10; int b = 20; cout << ((a > b) ? a : b) << endl; }
See output here.
Here, we are directly checking if "a" is greater then "b" then return "a" to output stream (cout) and if not return "b" to output stream (cout).
For the complete list of operators click here.
In the next slide we will
Stay tuned for the upcoming content. For any query leave a comment below.
- Lippman, Stanley B. c++ primer 3rd edition(april 2, 1998)
- Wikipedia
- Hilite.com for code snippets
- Ideone.com (for ability to share codes online).
Comments
Post a Comment