Sequential Loops
A Programming Pattern
Prof. David Bernstein
James Madison University
Computer Science Department
bernstdh@jmu.edu
Motivation
A Review:
Loops allow us to repeat a step or sequence of steps multiple times
An Observation:
We can't always accomplish what we want/need to with a single loop
Using Multiple Loops
Nested
:
One (or more) loops in the body of another loop
Sequential:
One (or more) loops following/after another loop
When the Need Arises
After Using an
Accumulator
:
Bigger/smaller than the average
Percent of the total/minimum/maximum
After Finding a
Containing Interval
:
Position within an interval
An Example
The Problem:
Display the elements of an array that are larger than the mean value
A Solution (using Sequential Loops):
javaexamples/programmingpatterns/SequentialLoopExample.java (Fragment: big)
There's Always More to Learn