When repeating the same action over and over again, a human is likely to make a mistake. Computers, however, possess the incredible ability to carry out repetitive tasks without making mistakes.
To see this, let’s consider an appropriate, if somewhat contrived, example. Suppose you want to print out the integers 0 through 50. With the tools you currently have at your disposal, your program would look like this:
Not only is this highly repetitive, but it is also error-prone. Even if utilizing copy-paste functionality, the sheer volume of code makes it somewhat likely that we will make a simple mistake, such as skipping an integer or misspelling
This code is also hard to modify. If we want to make a conceptually simple change—such as printing all the way to 100, or only printing even numbers—then we are forced to update an immense amount of code. Programming languages provide tools that allow us to repeat a sequence of statements in a much simpler way.
To give you a taste of what’s to come, here is how we could write the program above using a
We will explore the details of this syntax shortly, but it’s worth taking a moment to marvel at the simplicity of this program compared to the one above.
It may seem odd to you that this loop uses the integer 51, but only prints up to 50. Why this is the case will become clear in the next section.
Learning about iteration using loops is also an opportunity to introduce one of the most widely-known mnemonic devices in programming: Don’t Repeat Yourself, or DRY. A common piece of advice from instructors and experienced programmers is that you should “keep your code DRY.” Let’s learn how.