Understanding Iteration
In Chapter 4, we learned about scope and conditional statements in Java. Scope determines the visibility of identifiers – in other words, where you can use them. Java uses block scope, which is defined by curly braces, {}
. Scopes can be nested but not vice versa.
We discussed variations of the if
statement. Each of these statements evaluates a boolean condition, resulting in true or false. If true, then that branch is executed and no other branch is evaluated. If false, then the next branch is evaluated. Unless an else clause is present, it is possible that no branch at all will be executed.
For complex if
statements, Java supports the more elegant switch
structure. We examined switch
statements, with their fall-through behavior, and the use of the break
statement. In addition, we discussed switch
expressions, where a value can be returned, and their use of yield
.
Now that we understand conditional logic, let us examine iteration (looping). Looping...