Learn To Code With Loops

Eat. Sleep. Code. Repeat. This whatchamacallit can make a developer smile or smirk. For those who experienced it once or so in their career, it can both be both funny and, well, not so funny at all. Whatever it means to you, just know instead that developers live, love and laugh just like everyone else. But I digress. The point at hand really is understanding what it means to "repeat".

In a cafeteria, you can find a salad section. You can tell the chef what ingredients you want, he mixes them all together, adds the dressing, gives it a toss and serves your salad in a bowl. Your friend will be next and the chef pretty much does the same thing: mixes the ingredients, adds the dressing, gives it a toss and serves in a bowl. Every customer gets the same experience: mix, dress, toss and serve. If you happen to be the last customer, after you get your salad, the chef ends the routine and starts cleaning up to end the day.

If you want to describe what the chef is doing, you can draw a diagram that illustrates his steps in a circle. The first step can be at 12 o'clock, clockwise to the next at 3 o'clock, and so on until you're back to step 1 at 12 o'clock to repeat the cycle. In programming, you can express the same routine in what's known as a loop.

Learning to loop is rewarding. You should find that most of the things you need to program can require a loop. You want to read a file from start to end? You'll need a loop. You want to write to a file from start to end? You'll need a loop. You want to do some actions on a list of objects? You'll need a loop. You want something done a finite number of times? You'll need a loop. You want something to happen forever? You need a loop.

In general, when you start a loop, you also want it to end. Remember, the chef stops mixing and dressing and tossing and serving when there is no more customer. In the same way, there should always be a reason to stop a loop. In all programming languages, looping constructs always provide a way to stop or break a loop. The for loop ends when the iterator reaches a limit. A foreach loop ends when the enumerator reaches the end of the list. A while loop ends when the while condition is no longer true. Explicitly breaking out of the loop is also possible, to allow your program to "escape" or "exit" a loop. Just remember, when you create a loop, always start thinking about how the loop can end. Otherwise, you increase the chance of getting an infinite loop.

An infinite loop is basically a loop that has no means to stop. In practice, even if you think you need an infinite loop, you still want to provide a way to stop it. Most of the time, an infinite loop is a bug. Consider writing a code like so: while(true) {}. If your compiler allows you to build and run this, your program will be trapped in an infinite loop. While true equals true, the program will keep on doing whatever instructions are between the curly brackets. In this case, nothing. At this point, the program will just keep on running forever doing nothing.

If I may digress again, let me tell you what I really think about "Eat. Sleep. Code. Repeat." The problem with this whatchamacallit is that it's actually an infinite loop. There is no escape. It's a situation you don't want to be in. It's a situation no one wants to be in. So next time you see that cool looking "Eat. Sleep. Code. Repeat." shirt, you can skip it, keep calm and learn to code instead.

Comments