JMU
Repetition and Looping
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
The Natural Approach to Repetition

An Example

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);

// ...
Shortcomings of this Approach
A Slightly Better Approach to Repetition

At Least We Can Cut and Paste (and Avoid Some Mistakes)

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);

// ...
Shortcomings of this Approach
Toward a Good Approach
Definitions
An Analogous Situation
Important Concepts
Important Concepts (cont.)
Common Uses of Looping/Repetition
while Loops
while Loops (cont.)

Syntax using a Railroad/Train Analogy

images/railroad_while-syntax.gif
while Loops (cont.)

Typical Usage using a Railroad/Train Analogy

images/railroad_while-typical.gif
while Loops (cont.)

An Example

javaexamples/basics/WhileExample1.java (Fragment: 0)
        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

javaexamples/basics/WhileExample1.java (Fragment: 1)
        //    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

javaexamples/basics/WhileExample1.java (Fragment: 2)
        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

javaexamples/basics/WhileExample1.java (Fragment: 3)
        // 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.)
http://www.smbc-comics.com/comics/20120628.gif
(Courtesy of Saturday Morning Breakfast Cereal)
do-while Loops
do-while Loops (cont.)

Syntax using a Railroad/Train Analogy

images/railroad_do-syntax.gif
do-while Loops (cont.)

Typical Usage using a Railroad/Train Analogy

images/railroad_do-typical.gif
do-while Loops (cont.)

An Example

javaexamples/basics/DoExample1.java (Fragment: 0)
        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.)

An Example of an Infinite do-while Loop

javaexamples/basics/DoExample1.java (Fragment: 1)
        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 Loops
for Loops (cont.)

Syntax using a Railroad/Train Analogy

images/railroad_for-syntax.gif
for Loops (cont.)

An Example

javaexamples/basics/ForExample1.java (Fragment: 0)
        //    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.)
Loops and Scope
Loops and Updating
Using Loops in Your Programs