Chapter 3
flow of control
Java programs start with first line of main and go step by step until the
end is reached
invoking methods alters the flow, as do conditionals and loops
conditionals allow us to choose which statement to execute next
if
if-else
switch
based on boolean expressions, evaluating to true or false
loops allows us to execute statements over and over again
while
do
for
also based on boolean expressions
if
if (boolean expression)
this line or group of lines in { } is executed
if the boolean expression is false, the line or group of lines in { } is
skipped all together
indentation is good programming practice
== and != test if two values are equal or not equal
other relational operators, figure 3.2, p. 113
lower precedence than arithmetic operators
listing 3.1, p. 112
if-else
if (boolean expression)
this line or group of lines in { } is executed
else
this line or group of lines in { } is executed
statements in { } are block statements
listing 3.2, 3.3 p. 116, 117
nested if
the statement in the else can be another if statement, called a nested if
listing 3.4, p. 118
switch
directs program to follow one of several paths based on a single value
based on cases
break statement causes execution to jump to the first statement after the
switch
without break, processing continues with next case, even if it wasnt true
if no case value is matched, a default case can be used
expression at top of switch must be integral data type (integer or char,
not boolean or float), and each case must be a constant
listing 3.5, p.121
switch used in place of huge nested if
logical operators
figure 3.3, p. 123
produce boolean results
! is logical NOT yields opposite value, but doesnt change the value of
the variable
&& is logical AND result is true if both operands are true,
otherwise is false
|| is logical OR result is true if either operand is true, otherwise is
false
NOT has highest precedence, then AND, then OR
they are short-circuited if the left operand is sufficient to decide the
boolean result, right operand is never even evaluated
comparing characters and floats
characters can be compared like integers since every character has an
associated integer value in the Unicode character set
dont use on Strings, though use String methods
comparing floats are interesting since they can be just slightly different
down many decimal places instead subtract their absolute values and compare
difference to some tolerance
prefix vs. postfix
++ is increment
-- is decrement
have very different meanings when used before
versus after a variable or expression
figure 3.7, p. 128
assignment operators, figure 3.8, p.129
evaluate entire expression on right first, then
used on the left
conditional
ternary operator (uses three operands)
boolean expression ? if true : if false
while
continuously evaluates a boolean expression, and while it is true, performs
the action(s) after it, then evaluates the boolean again
once false, the program continues after the while block
listing 3.6, 3.7,3.8, p. 133, 134, 136
make sure boolean expression eventually becomes false so loop will exit
otherwise infinite loop!
listing 3.9, p. 138
can have nested loops listing 3.10, p. 139
can use break to exit a loop to end it immediately
can use continue to stop that iteration of the loop and go reevaluate the
boolean expression
do
like while loop, but boolean expression is at end of loop, so always
executes at least once
listing 3.11, 3.12, p. 143, 144
for
good for loops that are running a known amount of time
listing 3.13, 3.14, 3.15, p. 146, 148, 150
has initialization, condition and increment, separated by semicolons
can use loops with graphics to make coding easier listing 3.17, 3.18,
3.19, p. 157, 159, 162
development stages
establish requirements
what program must accomplish
usually come from some other person or client
create design
how program will accomplish requirements
lay out classes and objects
design algorithm step-by-step process for solving problems
can use pseudocode here
implement code
writing the source code
testing implementation
run program and debug
example p. 152 156