For loop templates

Even more schematic view of the loop patterns in For loop patterns slides.

In the folowing slides everywhere there’s a ... you need to fill in something.

The patterns

Primordial, Counting, Summing, Maximizing, Minimizing, String building, String building with a condition, Index finding, Some, Every

The primordial for loop

for (int i = 0; i < n; i++) {
  // code here that uses i
}

You should be able to look at this and immediately know that it executes the body of the loop n times and that i takes on the values from 0 to n - 1, inclusive.

Back to list of patterns.

A counting for loop

int count = 0;
for (int i = 0; i < ...; i++) {
  if (...) {
    count++;
  }
}
return count;

We only count things that satisfy some condition, implemented in the if statement.

Back to list of patterns.

A summing for loop

int sum = 0;
for (int i = 0; i < ...; i++) {
  sum += ...
}
return sum;

We add to the sum each time through the loop but how much depends somehow on the loop variable.

Back to list of patterns.

A maximizing for loop

int maximum = Integer.MIN_VALUE;
for (int i = 0; i < ...; i++) {
  maximum = Math.max(maximum, ...);
}
return maximum;

By starting maximum Integer.MIN_VALUE, we ensure that the initial value is less than or equal to any value produced by the ... in the call to Math.max.

Back to list of patterns.

A minimizing for loop

int minimum = Integer.MAX_VALUE;
for (int i = 0; i < ...; i++) {
  minimum = Math.min(minimum, ...);
}
return minimum;

By starting minimum at Integer.MAX_VALUE, we ensure that the initial value is greater than or equal to any value produced by the ... in the call to Math.min.

Back to list of patterns.

String building for loop

String result = "";
for (let i = 0; i < ...; i++) {
  result += ...
}
return result;

Like the summing loop, we add something to our result string every time through the loop.

Back to list of patterns.

String building for loop with a condition

String result = "";
for (int i = 0; i < ...; i++) {
  if (...) {
    result += ...
  }
}
return result;

In this variant, we only sometimes add something to the result string.

Back to list of patterns.

An index finding for loop

for (int i = 0; i < ...; i++) {
  if (...) {
    return i;
  }
}
return -1;

-1 is a good value to return if we don't find what we're looking for because it can't possibly be an actual index since indices start at 0 and go up.

Back to list of patterns.

A “some” for loop

for (int i = 0; i < ...; i++) {
  if (...) {
    return true;
  }
}
return false;

As soon as we find one example of what we're looking for we know there is at least one of what we're looking for so we can return true immediately. But if we finish looping and haven't returned then we know there are none and return false

Back to list of patterns.

An “every” for loop

for (int i = 0; i < ...; i++) {
  if (...) {
    return false;
  }
}
return true;

As soon as we find one counterexample we know that not every case matches our conditionso we can return false immediately. But if we finish looping and haven't returned then we know there were no counter examples and can return true.

Back to list of patterns.