For loop patterns

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.

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.

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.

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.

This does have the problem that if the loop doesn’t run at all then the result will be MIN_VALUE which may or may not be okay.

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.

As with the maximizing loop, this also has the problem that if the loop doesn’t run at all then the result will be MAX_VALUE which may or may not be okay.

String building for loop

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

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

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.

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 indexes start at 0 and go up.

A “some” or “any” 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

An “every” or “all” 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.