Repetition and Looping
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
feetPerKnot = 6080.0; feetPerMile = 5280.0; speedInKnots = 1.0; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); speedInKnots = 2.0; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); speedInKnots = 3.0; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); speedInKnots = 4.0; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); // ...
feetPerKnot = 6080.0; feetPerMile = 5280.0; speedInKnots = 0.0 ++speedInKnots; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); ++speedInKnots; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); ++speedInKnots; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); ++speedInKnots; speedInMPH = speedInKnots * feetPerKnot / feetPerMile; JMUConsole.printf("%5.2f\n", speedInMPH); // ...
while
Loopswhile
Loops (cont.)Syntax using a Railroad/Train Analogy
while
Loops (cont.)Typical Usage using a Railroad/Train Analogy
while
Loops (cont.)
speedInKnots = 1.; while (speedInKnots < 50.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; }
while
Loops (cont.)while
Loops (cont.)An Example with 0 Iterations
// A while loop with 0 iterations speedInKnots = 51.; while (speedInKnots < 50.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; }
while
Loops (cont.)
An Example of an Infinite while
Loop
speedInKnots = 1.; while (speedInKnots > 0.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; }
while
Loops (cont.)
An Example of an Indefinite while
Loop
// Prompt the user and assign the response to speedInKnots while (speedInKnots > 0.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); // Prompt the user and assign the response to speedInKnots }
while
Loops (cont.)do-while
Loopsdo-while
Loops (cont.)Syntax using a Railroad/Train Analogy
do-while
Loops (cont.)Typical Usage using a Railroad/Train Analogy
do-while
Loops (cont.)
speedInKnots = 1.; do { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; } while (speedInKnots < 50.);
do-while
Loops (cont.)do-while
Loops (cont.)
do-while
Loop
speedInKnots = 1.; do { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; } while (speedInKnots > 0.);
for
Loopsfor
Loops (cont.)Syntax using a Railroad/Train Analogy
for
Loops (cont.)
// A simple for loop for (speedInKnots=0.; speedInKnots < 50.; speedInKnots=speedInKnots+1.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; JMUConsole.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); }
for
Loops (cont.)for
Loops (cont.)for (j=0; j<50; ++j)
for (int i=0; i<50; ++i)
i
and j
j
can be accessed outside of the
loop, i
can'tage = age + 1;
)age += 1;
)for
)
or indefinite (prefer while
or
do-while
)?do-while
)?