Programming Search⌘K

Loops

Iteration with counted loops, condition loops and loops over collections. The iterator protocols are in functions-and-abstraction.

Concepts

  • Do-while loop

    A do-while loop runs its body first and tests its condition after each pass, so the body always runs at least once. Pascal and Lua call it repeat-until.

    intro
  • For loop

    A for loop runs its body once for each value of a loop variable. The three-part form of C has an initialization, a condition and an update, and many languages loop over a range of numbers.

    intro
  • For-each loop

    A for-each loop runs its body once for each element of a collection, with no index to manage. Under it, the language asks the collection for an iterator and calls it until it has no more elements.

    intro
  • Loop

    A loop runs the same block of code again and again: a fixed number of times, while a condition is true, or once for each element of a collection.

    intro
  • While loop

    A while loop tests a condition before each pass and runs its body while the condition is true. It fits loops whose number of passes is not known at the start.

    intro
  • Infinite loops

    An infinite loop never ends by itself. Servers, event loops and game loops use one on purpose and leave it with break or return, and a bug in a loop condition makes one by mistake.

    core
  • Off-by-one errors

    An off-by-one error is a count or an index that is one too high or one too low, often a loop that runs one pass too many or too few. Half-open ranges prevent many of them.

    core
  • Iterator invalidation

    An iterator is invalidated when its collection changes in a way that the iterator cannot follow. Languages throw an error, skip elements, have undefined behavior or reject the code at compile time.

    advanced
  • Loop and a half

    A loop and a half runs the first part of its body n times and the second part n - 1 times, so its exit test belongs in the middle, written with break or with a read in the condition.

    advanced
  • Loop variable capture

    A closure made in a loop captures the loop variable, not its value. If the loop has one variable for all passes, every closure sees the last value. Languages differ, and Go and C# changed their rules.

    advanced

Tasks

  • FizzBuzz

    Count from 1 to n, and say Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for both, with the test for both before the other two.

    10 languagesintro
  • Loop over a collection

    Visit each item of a list with a for-each loop and add the items, and know which loops give the items, which give the keys, and when the sum needs a wider type.

    10 languagesintro
  • Loop over nested loops

    Search a grid with a loop inside a loop, stop both loops at the first match, and know which break leaves only the inner loop.

    10 languagesintro
  • Loop over several collections at once

    Walk three lists side by side in one loop, join the items at each index, and stop at the end of the shortest list instead of reading past the end of one.

    10 languagesintro
  • Loop with a step or downward

    Count from a start to a stop by a step that can be negative, include the stop when the loop reaches it, and keep the counter from overflowing near the largest integer.

    10 languagesintro
  • Loop with an index

    Loop over a list with the index and the item together, collect the index of each match, and avoid the loops that give only the first match or the wrong index.

    10 languagesintro
  • Write a counted loop

    Count from 1 to n with a loop and collect each number, and know which range forms include the end and which stop one before it.

    10 languagesintro
  • Write a while loop

    Halve a number until it is 0 with a while loop, collect each value, and see why the loop needs integer division and why a do-while loop gives a different result for 0.

    10 languagesintro