Implement counted loops.

Closes #120.