Arithmetic and Boolean Operations

Tom Kelliher, CS 116

Oct. 18, 2000

Administrivia

Announcements

Assignment

Nothing new. Should have read up to 5.4.

From Last Time

Scope and access.

Outline

  1. Arithmetic operators.

  2. Logical operators.

  3. Return exam.

Coming Up

Logical operations, assignments, statements.

Arithmetic Operators

Suppose we know the following:

int i = 5;
int j = -3;
int k = 12;
What are the values of the following expressions?
k - i * 2

i - 4 - j

4 / i * 5

i - 2 * k + j
Why?

How can we change the order of evaluation?

Arithmetic Operators

  1. Binary operators: +, -, *, /, %.

  2. Unary operators: -, ++, -- (and casts).

    Use increment and decrement judiciously.

    Unary arithmetic operators have higher precedence than binary.

    Associativity?

Math Class Methods

abs(), ceil(), floor.

Logical Operators

Comparison and boolean operators.

Arithmetic Comparison Operators

<, <=, ==, !=, >, >=.

  1. Lower precedence than binary arithmetic operators.

    Equality, inequality one level below others.

  2. Associate left-to-right.

  3. Take numeric arguments. Return a boolean.

    Why is 0.0 < x < 1.0 illegal?

Examples. Assume:

int    n = 3;
double x = 4.8;
Evaluate (explain):
n < 3

n <= 3

n < x

(n + 3) == 2

2 * n != x - 1.0

n >= n

n / 4 > 0

n >= n == n < 3

Logical Operators

  1. Take boolean argument(s). Return a boolean value.

  2. Binary: &&, ||.

    Lower precedence than equality, inequality.

    Associate?

  3. Unary: !.

    Unary precedence.

    Associate?

  4. Deriving the truth tables.



Thomas P. Kelliher
Tue Oct 17 19:20:01 EDT 2000
Tom Kelliher