Java Lecture 7
Other Selection and Repetition Statements
Logical and assignment operators (pp181-186)
- &&, ||, and ! --> And, Or, and Not
- ++ incrementing operator (and --, the decrementing operator)
- Various <op>= assignments, like +=, -=, *=
The switch statement (p 189)
- Allows multi-way selection, rather than just two-way selection like
the if-else statement
- Selection value is an integer, rather than a boolean test
- Multiple "cases" are defined, one for each
value that is expected
- the "break" statement must be the last statement
in each case, if they are to be independent
- For all other (non-expected) values, the "default"
case is executed.
- An example program.
The for loop (p 196)
- Allows compact counting-style loops
- Initialization, loop test, and incrementing statement are all part
of the loop header
- The initialization is executed just once. It is as if the initialization
statement was a statement just before a while loop
- The loop test is just like the while loop -- it must be true to execute
the loop body
- The incrementing statement is executed after the loop body. It is as
if the incrementing statement was the last statement in a while loop body
- Very handy for counting through arrays, but remember that the test
can be any boolean expression, not just a test for the value of the counter.
The do-while loop (p 194)
- This loop puts the loop test at the bottom of the loop rather than
the top.
- The loop test is executed after the loop body, and, if true, the body
is executed again
- Very important: the body of the do-while loop is always
executed at least once!
Example loops in a program