Java Lecture 7

Other Selection and Repetition Statements

Logical and assignment operators (pp181-186)

  1. &&, ||, and ! --> And, Or, and Not
  2. ++ incrementing operator (and --, the decrementing operator)
  3. Various <op>= assignments, like +=, -=, *=

The switch statement (p 189)

  1. Allows multi-way selection, rather than just two-way selection like the if-else statement
  2. Selection value is an integer, rather than a boolean test
  3. Multiple "cases" are defined, one for each value that is expected
    1. the "break" statement must be the last statement in each case, if they are to be independent
  4. For all other (non-expected) values, the "default" case is executed.
  5. An example program.

The for loop (p 196)

  1. Allows compact counting-style loops
  2. Initialization, loop test, and incrementing statement are all part of the loop header
  3. The initialization is executed just once. It is as if the initialization statement was a statement just before a while loop
  4. The loop test is just like the while loop -- it must be true to execute the loop body
  5. 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
  6. 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)

  1. This loop puts the loop test at the bottom of the loop rather than the top.
  2. The loop test is executed after the loop body, and, if true, the body is executed again
  3. Very important: the body of the do-while loop is always executed at least once!

Example loops in a program