CH 04
CH 04
• Syntax:
target = expression;
type
Examples:
int numPlayers = 10; // numPlayers holds 10
numPlayers = 8; // numPlayers now holds 8
int legalAge = 18;
int voterAge = legalAge;
• The next statement is illegal
int height = weight * 2; // weight is not defined
int weight = 20;
• Generates the following compilation error:
cannot resolve symbol
Arithmetic Expressions
• An expression is a combination of one or more
operands and their operators
• Arithmetic expressions compute numeric results and
make use of the arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
If either or both operands associated with an
arithmetic operator are floating point, the result is a
floating point
Arithmetic Operators
Operator Operation
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder
after division)
Arithmetic Assignment Operators
Operator Example Equivalent
+= a += 3; a = a + 3;
-= a -= 10; a = a - 10;
*= a *= 4; a = a * 4;
/= a /= 7; a = a / 7;
%= a %= 10; a = a % 10;
Modulus Operator
• When dividing two integers:
• the quotient is an integer
• the remainder is truncated (discarded)