Iterative Loops: Loops 101
Iterative Loops: Loops 101
Everyone has some sort of daily routine. Get up, brush teeth, train squirrels to perform circus tricks, and so on. Sure, details might change depending on the day of the week or the season (those squirrels aren't going to put doll-sized scarves on themselves, you know), but there's almost always some kind of uniformity between different days. It's the stuff montages are made of.
Unlike how boring and repetitive daily routines feel, when a computer loops, it's usually a good thing. Loop is the non-technical term for a place where a specific block of code repeats itself zero or more times—but usually more. If you've got some task that repeats the same group of instructions over and over again in a program, like
- adding two numbers,
- reversing the pixels in an image,
- replacing a character or word,
- displaying a menu,
you'll want to write a loop for that part. Loops solve big problems by breaking them down into repeatable subproblems. Need to figure out how many squirrels can successfully perform a specific circus routine? Break it down into a repeatable subproblem of figuring out whether an individual squirrel can perform that routine. Then repeat it on another squirrel until there are no more squirrels to test. Just an average day in the life of a programmed loop.
There are two main types of loops:
- Iterative loops
- Recursive loops
Iteration happens when a piece of code runs zero or more times—just like you'd expect from a loop. Usually this kind of loop has some slight change at each iteration (run-through of the loop), until some kind of end condition is met.
Recursive loops are similar in that they also run the same code over and over, but the loop happens because the piece of code calls itself (which could call itself again), waits for the call to itself to end, and then keeps going. We're going to focus solely on iterative loops (check the name of the guide), but there's also a guide about the iterative loop's sib from another crib (recursion).
A Case for Lists
Loops (whether iterative or recursive) can be good at shortening both the amount of code required to do something and the time it takes to run that code. For example, running through all the elements of a list without a loop would be extremely difficult. Without a loop to stop after an unspecified number of iterations, you'd probable need a different variable for each possible element in the list (which kind-of defeats the purpose of a list, if you ask us) and a block of code that has whatever you want to do to the elements written out for each element.
Since lists can be really, really, really long, the number of variables you'd need to keep track of to find the size would have to be just as long. Imagine having to come up with names for all the elements in a relatively small list of size 100. You'd run out of alphabet by the time you were a quarter way through. Then what?
With a loop, though, you could walk down the list from head to tail without worrying about using the copy and paste shortcuts on your keyboard. That's a good thing for both saving your fingers from carpal tunnel and saving your eyes from hours of debugging.
Types of Iterative Loops
Iterative loops can be subdivided into for and while loops. The syntax in every language is a little different, but just about all of them have these two main types of loops. They can be used interchangeably, so you could argue (and probably win) and argument that you really only need one type of loop, but…sometimes it's much easier to use one type of loop than it is to use the other.
For loops have a very specific amount of time that they can run. In their creation, a programmer has to tell the computer exactly how many times that block of code should run. In contrast, while loops can run as many times as they want without relying on some explicit number of iterations listed by the code.
In general, loops allow you to offload some of your work onto the computer, but at a small cost. The longer and more complex your loops are, the longer it'll take for them to finish up. After all, a bunch of small costs build up. Write a loop too long/complex and you might be dead before it finishes. Seriously.
But no pressure.