Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
C++ is rich in built-in operators and provide the following types of operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one.
Arithmetic Operators:
Operations of addition, subtraction, multiplication and division literally correspond with their respective mathematical operators. The only one that you might not be so used to see is modulo; whose operator is the percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if we write
a = 11 % 3;
the variable a will contain the value 2, since 2 is the remainder from dividing
11 between 3.
Ther are following arithmetic operators supported by c++:
Picture
Relational operater:-
Inorder to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result.
Operator | Use | Description |
---|---|---|
> | a > b | Returns true if a is greater than b. |
>= | a>=b | Returns true if a is greater than or equal to b. |
< | a < b | Returns true if a is less than b. |
<= | a <=b | Returns true if a. is less than or equal to b. |
== | a==b | Returns true if a and b are equal. |
!= | a!=b | Returns true if a and b are not equal |
Logical operators ( !, &&, || )
The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.Example of Logical Operators
#include <iostream> using namespace std; int main(){ bool b1= true; bool b2= false; cout<<"b1 && b2: "<<(b1&&b2)<<endl; cout<<"b1 || b2: "<<(b1||b2)<<endl; cout<<"!(b1 && b2): "<<!(b1&&b2); return 0; }
Output:
b1 && b2: 0 b1 || b2: 1 !(b1 && b2): 1
Bitwise Operators ( &, |, ^, ~, <<, >> )
Bitwise operators modify variables considering the bit patterns that representPicture
Assignment (=)
The assignment operator assigns a value to a variable. a = 5;This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these.
The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from right to left, and never the other way:
a = b;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was stored until this moment in a is not considered at all in this operation, and in fact that value is lost.
Miscellaneous Operators
There are few other operators in C++ such as Comma operator and sizeof operator. We will cover them in detail in a separate tutorial.
Operator Precedence in C++
This determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.
OicPictu
No comments:
Post a Comment