© 2006 pearson education 1 obj: to use assignment operators hw: prelab exercises 2 quiz 3.1 – 3.4...

6
1 © 2006 Pearson Education Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days Do Now: 1. Read p.144 – 145 (3.4 “more operators”). 2. Write two different lines of code that will both add 1 to the value of the variable total. 3. Write two different lines of code that will both add 7 to the value of the variable total. C3 D3b C3 D3b

Upload: helen-crawford

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

1© 2006 Pearson Education

Obj: to use assignment operators

HW: Prelab Exercises 2

Quiz 3.1 – 3.4 in two class days

Do Now:

1. Read p.144 – 145 (3.4 “more operators”).

2. Write two different lines of code that will both add 1 to the value of the variable total.

3. Write two different lines of code that will both add 7 to the value of the variable total.

C3 D3bC3 D3b

Page 2: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

2© 2006 Pearson Education

Increment and DecrementIncrement and Decrement

The increment and decrement operators are arithmetic and operate on one operand

The increment operator (++) adds one to its operand

The decrement operator (--) subtracts one from its operand

The statement

count++;

is functionally equivalent to

count = count + 1;

Page 3: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

3© 2006 Pearson Education

Assignment OperatorsAssignment Operators

Often we perform an operation on a variable, and then store the result back into that variable

Java provides assignment operators to simplify that process

For example, the statement

num += count;

is equivalent to

num = num + count;

Page 4: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

4© 2006 Pearson Education

Assignment OperatorsAssignment Operators

There are many assignment operators, including the following:

Operator

+=-=*=/=%=

Example

x += yx -= yx *= yx /= yx %= y

Equivalent To

x = x + yx = x - yx = x * yx = x / yx = x % y

Page 5: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

5© 2006 Pearson Education

Assignment OperatorsAssignment Operators

The entire right-hand side of an assignment expression is evaluated first, then the result is combined with the original variable

Therefore

result /= (total-MIN) % num;

is equivalent to

result = result / ((total-MIN) % num);

Page 6: © 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more

6© 2006 Pearson Education

Try this:Try this:

p.180 Multiple Choice #3.1

If time: begin “Processing Grades” Lab.