Arithmetic Operators and
Operator Precedence
C++ arithmetic operators:
−+
addition
−-
subtraction
−*
multiplication
−/
division
−%
modulus operator
+, -, *, and / can be used with integral
and floating-point data types
Operators can be unary or binary
Order
of Precedence
All operations inside of () are
evaluated
first
*, /, and % are at the same level of
precedence and are evaluated
next
+ and – have the same level of
precedence
and are evaluated last
When operators are on the same
level
−Performed
from left to right
(associativity)
3
* 7 - 6 + 2 * 5 / 4 + 6
means
(((3 * 7) – 6) + ((2 * 5) /
4
)) +
6
Expressions
•If all operands are integers
−Expression
is called an integral expression
•Yields
an integral result
•Example:
2 + 3 * 5
•If all operands are floating-point
−Expression
is called a floating-point expression
•Yields
a floating-point result
•Example:
12.8 * 17.5 - 34.50
Mixed
Expressions
•Mixed expression:
−Has
operands of different data types
−Contains
integers and floating-point
•Examples of mixed expressions:
2 + 3.5
6
/ 4 + 3.9
ليست هناك تعليقات:
إرسال تعليق