CIS 15AG
Review Chapter 3:
Structure of a C Program: Expressions and Statements
Expression
– a sequence of operands and operators that reduces to a single value, such
as:
40 + 50
or
5 + 12 / 3
Division and natural numbers
– if
num
and
d
are natural numbers, with
d
nonzero,
there exist unique integers
q
and
r
such that
num
=
q
·
d
+
r
, and 0
≤
r
<
d
.
num
–
dividend
,
d
–
divisor
,
q
–
quotient
,
r
–
remainder
. For instance, when dividing
9
by
4
, the quotient is
2
and the remainder is
1
:
9 = 2·4 + 1
Division and integers
– quotient and remainder are not unique. Examples:
Dividend
Quotient
Divisor
Remainder
9 /
4
9 =
2
·
4 +
1
9 /
4
9 =
(2)
·
4 +
(1)
9 =
(3)
·
4 +
3
9 / 4
9 =
(2)
·(4)+
1
9 =
(3)
·(4)+
(3)
9 / 4
9 =
2
·(4)+
(1)
9 =
3
·(4)+
3
In number theory, usually the positive remainder is chosen, but not in programming.
Some programming languages choose the remainder with the same sign as the dividend
(Pascal, C99), others with the same sign as the divisor (Perl, Python).
Arithmetic Operators
+
add
9
+
4

subtract
9

4
4

9
*
multiply
9
*
4
/
divide
9
/
4
=> 2
//
Integer result if both operands are integer
9
/
4.0 => 2.25
9.0
/
4
=> 2.25
9.0
/
4.0 => 2.25
// Floatingpoint result if either operand is a
//
floatingpoint number
%
modulus
 produces the remainder
8
%
4
0
9
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 Staff
 Computer Programming, Assignment Operator, arithmetic operators, implicit type conversion

Click to edit the document details