Академический Документы
Профессиональный Документы
Культура Документы
Operator Meaning
∗ multiplication
/ Division
% Modulo division
Example 8
If a and b are two integers then:
a + b gives the sum of two integers a and b.
a * b gives their multiplication.
a / b gives the quotient when a is divided by b.
a % b gives the remainder when a is divided by b.
Example 9
Expressions
Any arrangement of variables, constants and operators that specifies a
computation is called an expression. Thus
Sum + I j+6 total/6 I + k*p height%22
are all examples of expressions. When computations specified in the
expressions are performed, the result is usually a value. Thus if the
values of Sum and I are 45 and 10 respectively, the value of that
expression is 55.
Parts of expressions may also be expressions. Even single variables and
constants, like total and 6, are considered to be expressions.
Note that expressions are not the same as statements. Statements tell the
compiler to do something and terminate with a semicolon, while
expressions specify a computation. There can be several expressions in a
statement.
Distance = (x1*x1 – y1*y1) + (x1*x1 – y1*y1) + val*654;
Integer arithmetic
When both the operands in a single arithmetic expressions such as a + b
are integers, the expression is called an integer expression. And
the operation is called integer arithmetic. Integer arithmetic
always gives integer value.
Real arithmetic
An arithmetic operation involving only real operands is called real
arithmetic. Since floating point values are rounded to the number of
significant digits permissible, the final values is an approximation of the
correct result.
Mixed-mode arithmetic
When one of the operands is real and the other is integer, the expression
is called a mixed-mode expression. If one of the operand is of the
real type, then only real operation is performed and the result is always a
real number.
Thus,
15/10.0 = 1.5
But
15/10 = 1
Relational operators
We often compare two quantities and depending on their values, take
certain decisions. These comparisons can be done with the help of
relational operators.
Operator Meaning
== is equal to
!= is not equal to
Logical operators
C++ has following logical operators.
Operator Meaning
|| Means logical OR
Example 10
Increment and decrement operators
C++ allows two very useful operators. These are the increment and
decrement operators:
++ and --
The operator ++ adds 1 to the operand, while -- subtracts 1. Both are
unary operators and takes the following form:
++m; or m++;
--m; or --m;
++m or m++ is equivalent to m = m + 1;
--m or m-- is equivalent to m = m - 1;
While ++m and m++ mean the same thing when they form
statements independently, they behave differently when they are
used in expressions on the right-hand side of an assignment
statement.
Example 11