ch6 - Chapter 6 Repetition Topics Some additional operators...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 Repetition
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Some additional operators increment and decrement assignment operators Repetition while do-while for Random numbers
Background image of page 2
Increment and Decrement Updating a variable by adding or subtracting 1 is a very common operation i = i + 1; j = j - 1; Java has operators that allow you to write this more compactly i++ and ++i for incrementing j-- and --j for decrementing Both operators can come either before (prefix) or after (postfix) the operand.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Increment and Decrement In a statement by themselves, the prefix and postfix operators do the same thing. These operators can be embedded in an expression. The variable being incremented gets updated at different times for the two cases prefix : increment/decrement performed first postfix : increment/decrement done last
Background image of page 4
Increment and Decrement If count currently contains 45, then the statement total = count++; assigns 45 to total and 46 to count If count currently contains 45, then the statement total = ++count; assigns the value 46 to both total and count
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assignment Operators Another common operation consists of performing an operation on that variable and storing the result back in the same variable Java provides assignment operators to simplify this process For example, the statement num = num + count; can be replaced by num += count;
Background image of page 6
Assignment Operators Java also provides assignment operators for all the binary operations. x += a; add a to x and store the new value x -= a; subtract a from x and store the new value x *= a; multiply x by a and store the new value x /= a; divide x by a and store the new value i %= j; replace i by i % j
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assignment Operators The right hand side of an assignment operator can be a complex expression Semantics of assignment operators: The entire right-hand expression is evaluated Then the result is combined with the original variable For example result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);
Background image of page 8
Assignment Operators The behavior of some assignment operators depends on the types of the operands If the operands to the += operator are strings, the assignment operator performs string concatenation The behavior of an assignment operator ( += ) is always consistent with the behavior of the "regular" operator ( + ) The /= operator does integer or floating point division depending on its operands.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Repetition Repetition statements control a block of code to be executed for a fixed number of times or until a certain condition is met. Java has three repetition statements: while do-while for
Background image of page 10
The while Statement In Java, while statements follow a general format: while ( <boolean expression> ) <statement> For example: int sum = 0, number = 1; while (number <= 100){ sum = sum + number; number = number + 1; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while Statement Repetition statements are also called loop statements , and the
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/18/2011 for the course COMPUTER S CSC113 taught by Professor Shah during the Spring '09 term at King Saud University.

Page1 / 52

ch6 - Chapter 6 Repetition Topics Some additional operators...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online