Java Reference
In-Depth Information
Summary
This chapter covered what many programmers consider to be the main purpose of programming:
identifying conditions and doing appropriate things for each one. If we have no ability to change what
we do based on the inputs we receive, no program could achieve the complexity to do anything useful.
To that end, every programming language offers at least a few ways to change paths within the code.
As we learned in this chapter, Java offers:
if
and
if
-
else
(and
else if
) statements, for simple comparisons
•
•
switch
blocks, for comparing lots of values
for
(and enhanced for or “for each”) loops, for easy-to-read looping
•
while
loops, for when we want to do something until a condition is met
•
•
do
-
while
loops, for when we want to have the test come last
break
statements, for jumping out of the current block
•
•
continue
statements, for moving on to the next item
•
return
statements, for saying we're done with this method and returning a value
Thanks to all these possibilities for controlling the flow of our logic, we can write programs that can
handle any number of inputs and outputs and intermediate processing steps correctly and still be easy-
to-read.