- Forward


Repetition and Looping
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Algorithms often need to repeat a step or a sequence of steps
    • Example: Check all batting averages to find the player with the highest
  • We often want to apply the same algorithm multiple times
    • Example: Convert multiple speeds in knots to the equivalent speeds in mi/hr (e.g., to create a table)
Commonalities Across these Situations
Back SMYC Forward
  • We start by initializing a variable that gets used in one or more calculations
  • We perform the calculation
  • We update the variable that gets used in the calculation
  • We determine if we're done
Definitions
Back SMYC Forward
  • Loop:
    • A portion of a program that repeats a statement or group of statements
  • Body of a Loop:
    • The statement or group of statements that is repeated
  • Iteration:
    • One "pass" through the body of a loop
Important Concepts
Back SMYC Forward
  • Termination/Exit Condition:
    • Can involve a pre-test (i.e., a test at the top) or a post-test (i.e., a test at the bottom)
  • Sentinel:
    • A value that would normally be "invalid" that is used to signal that a loop should terminate
Important Concepts (cont.)
Back SMYC Forward
  • Infinite Loop:
    • A loop that never terminates
    • http://get.unshelved.com/strips/20071029.gif
      (Courtesy of Unshelved)
      Expand
  • Indefinite Loop:
    • A loop that terminates after a number of iterations that is unknown a priori
while Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • while (boolean_expression) statement
  • Note:
    • statement can be a simple or block statement
while Loops (cont.)
Back SMYC Forward

An Example

speedInKnots = 1.; while (speedInKnots < 50.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; System.out.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; }
while Loops (cont.)
Back SMYC Forward
  • Properties:
    • Use a pre-test
    • Use a sentinel
  • Implications:
    • May have 0 iterations
    • May be infinite
    • May be indefinite
while Loops (cont.)
Back SMYC Forward
http://www.smbc-comics.com/comics/20120628.gif
(Courtesy of Saturday Morning Breakfast Cereal)
do-while Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • do statement while (boolean_expression);
  • Note:
    • statement can be a simple or block statement
do-while Loops (cont.)
Back SMYC Forward

An Example

speedInKnots = 1.; do { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; System.out.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); speedInKnots = speedInKnots + 1.; } while (speedInKnots < 50.);
do-while Loops (cont.)
Back SMYC Forward
  • Properties:
    • Use a post-test
    • Use a sentinel
  • Implications:
    • Must have one iteration
    • May be infinite
    • May be indefinite
for Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • for (initialization; boolean_expression; update) statement
  • Note:
    • statement can be a simple or block statement
for Loops (cont.)
Back SMYC Forward

An Example

for (speedInKnots=0.; speedInKnots < 50.; speedInKnots=speedInKnots+1.) { feetPerHour = speedInKnots * feetPerKnot; speedInMPH = feetPerHour / feetPerMile; System.out.printf("knots: %5.2f\tmph: %5.2f\n", speedInKnots, speedInMPH); }
for Loops (cont.)
Back SMYC Forward
  • Properties:
    • Use a pre-test
    • Use a "built-in" initialization
    • Use a "built-in" sentinel
  • Implications:
    • May have 0 iterations
    • May be infinite
    • Are most frequently definite (i.e., not indefinite)
for Loops (cont.)
Back SMYC Forward
  • An Observations:
    • Arrays have a known and fixed length so are often processed using for loops
  • Example:
    • for (int i=0; i<args.length; i++) { System.out.printf("Argument %d is: %s\n", i, args[i]); }
Loops and Scope
Back SMYC Forward
  • Consider These Examples:
    • for (j=0; j<50; ++j)
    • for (int i=0; i<50; ++i)
  • What's Different?
    • The scope of i and j
      Expand
  • Why Does It Matter?
    • j can be accessed outside of the loop, i can't
      Expand
  • What About the Other Kinds of Loops?
    • The same kind of issue can arise though it is less common
Loops and Updating
Back SMYC Forward
  • An Observation:
    • The update pattern is commonly used in loops
  • Which Approaches?
    • Explicit updates (e.g., age = age + 1;)
    • Compound assignment operators (e.g., age += 1;)
Java vs. Python - Important Differences
Back SMYC Forward
  • Parentheses:
    • In Java, parentheses are required
  • Colons:
    • In Python, the expression/range/condition is followed by a colon
  • Indenting:
    • In Python, indenting is syntactically significant
Java vs. Python - Important Differences
Back SMYC Forward
  • Looping over a Range:
    • In Python, a for loop can be provided a range of integers using the range() function
  • Looping Over a List/Array/String:
    • In Python, a for loop can be provided a list/array/string
    • In Java, it is possible to loop over a "collection", but we won't discuss how until later in the semester
  • Do Loop:
    • In Python, there is no equivalent of a do loop
There's Always More to Learn
Back -