# Arithmetic Expressions

```Arithmetic Expressions
•
•
•
•
Subtraction (-)
Multiplication (*)
Division (/)
– Integer
– Real Number
• Mod Operator (%)
•
•
•
•
Same as regular
Same as regular
Same as regular
Depends on the types
of the operands
involved.
• Remainder operator
Order of Operations
• Same as math class
– ()
– *, /, %
– +, -
• Here’s an example:
– 3 + 4*5 - 6/3*4/8 + 2*6 - 4*3*2
– 3 + 20 - 1 + 12 – 24
– 10
Integer Division
• If the two operands are of type int, then
integer division occurs
• An integer division truncates (chops off)
any fractional part of the answer.
• Examples
– 13/4 = 3
– 7/8 = 0
– 19/3 = 6
Real Number Division
• If at least one of the two operands is a
double (or float), then a real number
division occurs
• Examples
– 13/4.0 = 3.25
– 13.0/4.0 = 3.25
– 19.0/5 = 3.8
Other Issues with Division
• Variable on the left-hand side of an
assignment is of type int, while the
expression on the right is a real number
– val = 8/5.0, will set val to 1, if val is an int.
• If the variable on the left-hand side is a
double, but the expression on the right is
an int, the variable gets set to an int.
– val = 8/5, will set val to 1, if val is a double.
Mod Operator
• A % B returns the remainder when A is
divided by B, and A and B MUST BE ints!
• Examples
– 12%5 = 2
– 19%6 = 1
– 14%7 = 0
– 19%200 = 19
Initializing Variables
• If you declare a variable without a value,
then that variable initially could equal
anything
• Usually, it’s a good practice to give your
variables initial values.
• Examples
– int sum = 0, value = 1;
– double price = 0.0;
Defining Constants
• Use #define
• Examples
– #define FEET_IN_YARD 3
– #define PI 3.14159
• Benefits
– Code is Easier to Read
– If a constant needs to be “changed”, you only need to
change it at the top of your program. (ie. if you find
out that some program parameter has changed)
```