Even more schematic view of the loop patterns in Loop patterns slides.
In the folowing slides everywhere there’s a ...
you need to fill in something.
Primordial, Counting, Summing, Maximizing, Minimizing, String building, String building with a condition, Finding, Index finding, Some, Every
for
loopfor (let 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.
for
looplet count = 0;
for (let 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.
for
looplet sum = 0;
for (let 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.
for
looplet maximum = -Infinity;
for (let i = 0; i < ...; i++) {
maximum = Math.max(maximum, ...);
}
return maximum;
By starting maximum
at -Infinity
, we ensure that the initial value is less than any non-infinite value produced by the ...
in the call to Math.max
.
Back to list of patterns.
for
looplet minimum = Infinity;
for (let i = 0; i < ...; i++) {
minimum = Math.min(minimum, ...);
}
return minimum;
By starting minimum
at Infinity
, we ensure that the initial value is greater than any nond-infinite value produced by the ...
in the call to Math.min
.
Back to list of patterns.
for
looplet 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.
let result = '';
for (let 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.
for
loopfor (let i = 0; i < ...; i++) {
if (...) {
return ... // based on i, e.g. s[i]
}
}
return ...; // value for if we didn't find it
Back to list of patterns.
for
loopfor (let 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.
for
loopfor (let 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.
for
loopfor (let 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.