Title: Iteration Structures
1Lesson 7
2Iteration Structures
- Iteration is the third control structure we will
explore. - Iteration simply means to do something
repeatedly. - All iteration control structures test a condition
each time before the loop is executed.
3Types of Loops
- There are three types of loops that Java employs.
- Pretest Loop- test a condition each time before
the loop is executed. - Posttest Loop - test a condition after each loop
execution. - Fixed repetition - cause a loop to be executed a
predetermined number of times.
4Three Iteration Control Structures
- The three iteration control structures are the
while, do/while, and for. - The difference between them is the means by which
they control the exiting of the loop. - The while is a pretest loop.
- The do/while is a posttest loop.
- The for is a fixed repetition loop.
5The WHILE loop
- If the test expression is true the loop
statements are executed. - If the test expression is false the loop
statements are bypassed. - To get out of the loop something must change the
expression to false, otherwise the the result
will be an infinite loop.
6Syntax for the While Loop
while (test expression) statement1 statement2
statementn
7While Loop Example (counter)
- //Compute 1 2 . . . 100
- class counter
-
- public static void main (String Args)
-
- int sum 0, cntr 1
- while (cntr lt100)
-
- sum cntr
- cntr
- System.out.println (sum)
-
-
8While Loop Example (Square Roots)
- //Display the square roots of 25, 20, 15, and 10
- class SquareRoots
-
- public static void main (String args)
-
- int number 25
- while (number gt10)
-
- System.out.println ("The square root of "
number " is Math.sqrt(number)) - number -5
-
-
-
9The DO/WHILE loop
- The do/while loop is tested at the end of the
loop compared to the while which is tested at the
beginning. - The loop statements will always be executed once.
- To break the loop the test expression must become
false.
10Syntax for DO/WHILE
do statement1 statement2 statementn whil
e (test expression)
11- class dowhile
-
- public static void main(String args)
-
- int myNum
- //generate random numbers until we get 5
-
- do
- myNum (int)(Math.random() 10)
- System.out.print(myNum)
-
- while (myNum !5)
-
12Using Counters and Accumulators
- Numeric variables used within a repetition
structure to calculate subtotals, totals, and
averages - Counter
- Used for counting something
- Accumulator
- Used for accumulating (adding together) something
13Sentinel Values
- Values used to end loops
- Should be easily distinguishable from the valid
data used by the program - Also called trip values or trailer values
14Sentinel values
- Our program can read numbers repeatedly until it
encounters a special value called a sentinel that
marks the end of the list. - For example, if all the numbers in the list are
positive, then the sentinel could be -1.
15- import TerminalIO.KeyboardReader
- class sentinel
-
- public static void main(String args)
- KeyboardReader reader new KeyboardReader()
- double number, sum 0
- int count 0
- while (true)
- number reader.readDouble("Enter a positive
number or -1 to quit ") - if (number -1)
- break
- sum number
- count
-
- System.out.println("The average is "
sum/count) -
16The FOR loop
- The for loop runs a fixed number of times
- The first thing that is done is the
initialization of the counter. - If the results of the test expression is true,
then the statements are executed. - Each time the loop is executed, the loop counter
must be incremented or decremented.
17Syntax for FOR loop
for (initialize counter test counter update
counter) statement1 statement2 statementn
18- class Racer
-
- public static void main (String args)
-
- System.out.println("GO!")
-
- for (int lap1 lap lt10 lap)
-
- System.out.println("Completed " lap "
laps.") -
-
- System.out.println("Finish!")
-
-
19Nested Loops
- Many times it is desirable to have looping
operations within loops. - This is called nested looping.
- Each inner loop will as many times as desired X
the number of times the outer loop runs.
20Each iteration of the outer loop causes multiple
iterations of the inner loop.
- class NestedForLoop
-
- public static void main (String args)
-
- for (int i 0 i lt3 i)
-
- for (int j 0 j lt 3 j)
-
- System.out.println("i " i ", j "
j) -
-
-
-
-
21Break and Continue Option
- If you want a loop to terminate if some desired
value is found, it is possible to use the break
statement to force the loop to quit. - If you want one iteration of the loop to be
terminated, it is possible to use a continue
statement to force the loop to quit one iteration
and continue on with another.
22Syntax for BREAK
while (test expression) statement1 statement2
if (test expression) break statementn
23The loop will iterate once because the condition
is true, but the break statement takes the
program right out of the loop.
- class breakExample
-
- public static void main (String args)
-
- while (true)
-
- System.out.print ("true")
- break
-
-
- System.out.println(" out of loop")
-
24Syntax for CONTINUE
for (initialize counter test counter update
counter) statement1 statement2 if (test
expression) continue statementn
25The continue statement causes the loop to stop
where it is and loop again from the beginning,
causing an infinite loop.
- class ContinueExample
-
- public static void main (String args)
-
- boolean b true
- while (b)
-
- if (b) continue
- System.out.println("never gets here")
- b false
-
-
-
-
26Bottoms Up On The