Iterative Loops Introduction Introduction
You've just been hired as the new web designer for your school's athletics program. Sounds like a great resume builder and…it is, but there's one issue. Because of funding issues, the site's almost 10 years out of date and needs some serious remodeling. Not that there was anything wrong with styling from the early 10s, but…
For starters, you decide to replace the volleyball team's old name—The Sea Snakes—with the new one—The Manta Rays. Then you notice exactly how many pages have the old name: 28. Oh boy.
Just thinking about going through all the HTML pages by hand to change three words of text hundreds of times is tiring. Instead of doing it yourself, why not find a way to make the computer do it for you? You are an aspiring computer scientist after all, and computer science is all about making the computer do things for you.
Not only can you write code that automates the replacement of words (which is a standard function of most word processors, TBH), but you can automate how the code finds the words, making it loop until it's checked every single webpage—not just the 28 you found.
Yup, you can just write a loop—a repeating section of code—to go through a list of all the files you want changed and turn every instance of "Sea Snakes" to "Manta Rays." Write the code once and you can use it forever as your own, personal find and replace. Nifty.
There are different flavors of loops, too. Since you know exactly how many replacements are needed, you could use a for loop to run through your replacement code a specific number of times. If you're ever feeling a little uncertain, though, you can also write a while loop to run as long as it needs until it reaches an end condition.
Problem: solved.
…Until the next day, when you realize that the background colors should also be updated to match the new school colors of blue and orange instead of the previous icky shade of opaque couché and silver. Now that you've got all that handy knowledge of loops (and some basic understanding of hexadecimal colors), that site-saving pallet adjustment can happen literally in seconds.
Once that's done, you'll have way more time to add in all the cool new things that athletics program added in the past ten years, like quidditch and eSports.
Why Should I Care?
Time matters. Even the people who study queuing theory don't want to spend all of their time in line (which is why they study it, duh). Nobody wants to spend all their time waiting for something to happen, or doing busywork for funzies.
In computer science, time comes in two forms:
- Making the computer run the program faster
- Making code that the programmer can figure out quickly and modify easily
Not using loops doesn't matter much to the computer, but it matters a lot to the programmer. Writing the same lines of code over and over again—not to mention parsing through it to double check everything works—is a waste of time.
Loops help us gain back lost time or save time from the get-go. They're great for boosting efficiency and cutting down that write/run time. Loops are also versatile, seeing as they can be applied to the vast majority of coding problems. Got some code you want to run 5, 10, or 15 times? Just loop it. Even if you don't know how many times the code should run, as long as it should run more than once, loops can handle it.
Loops aren't the all-purpose cure to programming, though. They just run a block of code until you tell them to stop, which could be after a set amount of time, or…never. Because you can tell the loops to do a lot of your work for you, you have more free time to do other things with your life—like write more loops.
PSA: Please make sure to tell your loops to stop eventually, though. If they don't, bad things happen. Bad things like your computer becoming consumed by infinite loops and crashing because of it. So please be careful.