Forwardslash /

TAGS: wtf code

WTF Are Loops?

A loop is a fundamental programming construct that lets you run a chunk of code over and over. Loops are common to every programming language, and come in a few varieties: for loops, while loops, count loops, etc. The key component to loops is the control structure which tells them when to stop: otherwise, they’ll loop infinitely, which usually crashes your program and sometimes your computer.

Loops are a simple idea - it’s not hard to get the concept of something that repeats, going around and around again, like the physical loop on your bike chain. But when you’re just beginning to program, they can easily trip you up. My introduction to loops was in tangled PHP code (the WordPress loop). It’s easier to start with the clean, simple syntax of Python - then move on to things like the WordPress loop that have a few things in the mix at once.

In programming as in life, a small step repeated over and over is an incredibly powerful tool. Loops are one of the most basic instruments of a programmer, and once you get the gist of doing something repeatedly, the next step is doing something basically the same, but a little different each time. You can create smart loops that contain branching possibilities. You can nest loops within loops. Perhaps most commonly, you can process, or ‘iterate’ over a group of objects (such as bits of HTML content) and manipulate them in bulk to eliminate repetitive work. The possibilities of loops are, like themselves, potentially infinite.

Here’s a for loop in the most intuitive, compact language you can get (in Python).

for thing in list:
   print thing

Now, in practice:

shoppinglist = ['coffee', 'soap', 'razors']

for item in shoppinglist:
   print item

This outputs the following result:


This is very easy to follow and very logical. What’s hidden for you by the language is the mechanism that makes this work. Python doesn’t need to be told how long the list of items is, and to stop when it reaches the end.

Contrast that with a more traditional for loop, in JavaScript:

var shoppinglist = ['coffee', 'soap', 'razors'];

for (i = 0; i < shoppinglist.length; i++) {

This requires you to do some mental algebra, but shows you more of what’s going on with a loop. The for loop takes three parameters - three control switches in between semicolons in the parentheses. First, where to start the loop. Second, when to stop the loop. Third, how fast it should go and in what direction.

The “i” is typically used as an abbreviation for index. The three sections in the parentheses are setting three conditions for the for loop: first, setting the index to zero, then telling the loop to stop when it hits the length of the shopping list, then to count upwards one at a time - the i++ is a shortcut meaning count up once.

There’s a simpler kind of loop in JavaScript - the while loop. It looks like this.

var i = 0;

while (i < 200) {
	console.log("I am sorry I said those bad words");

This would write your line to the console 200 times. You can see all the elements from our for loop are here, just in different places. The counter is set to zero in the i variable. The condition when it stops is the single parameter given to the while. And the way it counts is set in the code that gets looped over.

Think for a moment of why this might be useful. What if you had a block of code that you wanted to run indefinitely, until something happened.

var headExploded = false;

while (headExploded == false) {

	headExploded = checkIfBrainIsFull();

Here we’re checking an imaginary function called checkIfBrainIsFull, that goes out and checks each time if there is a full brain. It keeps returning false until that condition is met, and our other fictional function called doMoreWork keeps running. (You would never write code this way, this is just to get the concept across.)

Go forth and loop!

TAGS: wtf code