Академический Документы
Профессиональный Документы
Культура Документы
com
Operators
Operators are special symbols used for specific purposes. C++ provides six
types of operators.
Arithmetical operators, Relational operators, Logical operators, Unary operators,
Assignment operators, Conditional operators, Comma operator
Arithmetical operators
Arithmetical operators +, -, *, /, and % are used to performs an arithmetic
(numeric) operation. You can use the operators +, -, *, and / with both integral
and floating-point data types. Modulus or remainder % operator is used only
with the integral data type.
Operators that have two operands are called binary operators.
Relational operators
The relational operators are used to test the relation between two values. All
relational operators are binary operators and therefore require two operands. A
relational expression returns zero when the relation is false and a non-zero
when it is true. The following table shows the relational operators.
== Equal to
!= Not equal to
Logical operators
The logical operators are used to combine one or more relational expression.
The logical operators are
Operators Meaning
|| OR
&& AND
! NOT
Unary operators
C++ provides two unary operators for which only one variable is required.
For Example
a = - 50;
a = + 50;
Here plus sign (+) and minus sign (-) are unary because they are not used
between two variables.
Assignment operator
The assignment operator '=' is used for assigning a variable to a value. This
operator takes the expression on its right-hand-side and places it into the
variable on its left-hand-side. For example:
m = 5;
The operator takes the expression on the right, 5, and stores it in the variable
on the left, m.
x = y = z = 32;
This code stores the value 32 in each of the three variables x, y, and z.
in addition to standard assignment operator shown above, C++ also support
compound assignment operators.
+= A+=2 A=A+2
-= A-=2 A=A-2
%= A%=2 A=A%2
/= A/ = 2 A=A/2
*= A*=2 A=A*2
The condition evaluates to false, therefore biggets the value from b and it
becomes 6.
Would first assign the value of a to i, and then assign value of b to variable i.
So, at the end, variable i would contain the value 2.
the sizeof operator determines the amount of memory required for an object at
compile time rather than at run time.
The order of Precedence
The order in which the Arithmetic operators (+,-,*,/,%) are used in a. given
expression is called the order of precedence. The following table shows the
order of precedence.
Order Operators
First ()
Second *, /, %
Third +, -
+, -
Lowest
<, <=, >, >=
==,!=
&&
?:
Comma operator