Write Your Code Once And Let It Run As Much As Needed
Did you ever watch Star Trek? I always preferred Star Trek over Star Wars even though Star Wars is probably more well known and popular. I found the topics and underlying themes of Star Trek much more enlightening and thought provoking.
One of my favorite episodes from The Next Generation is called “Cause and Effect”. It starts out with the crew struggling to keep their ship, the Enterprise, under control after some damage. The captain orders all hands to abandon ship right before it explodes.
Then Data is dealing cards and everything is back to normal. But the crew start noticing strange feelings of deja vu and the doctor starts hearing voices.
Sensors pick up a disturbance in space and the ship goes to investigate. Another ship emerges from the disturbance and is heading straight for the Enterprise. They collide and the Enterprise explodes again. But then everything goes back to the card game again with Data dealing.
They’re stuck in a repeating loop and need to figure out how to escape. And they keep doing the same things each time because they don’t know what else to do.
It’s a good example of what you can do with your code once you learn to write loops. A lot of people might think of a loop in a roller coaster when, really, the loop is better described by the entire track. The roller coaster starts out by picking up riders and moving along the track. It might swerve and turn and even go through some loops. But the real loop ends when the roller coaster comes back to the loading area and lets the passengers off. It starts the cycle over again when it picks up more riders and begins the whole track from the beginning.
So a loop is more than just turning around or turning upside down. It’s a way to repeat actions again and again until something happens to stop it. And it’s a way that you can write your code once and have it run over and over until some condition is met that ends the loop.
You’ll learn about for loops which are good for counting through items as the loop progresses.
- There’s three parts to this loop and it might take a little getting used to.
- An initializer sets things up and gets ready for the loop to begin.
- Criteria is used to determine how many times the loop should happen.
- A post action lets you perform something simple like incrementing the count of how many times you’ve already run through the loop.
- We’ll also talk about whether or not you should have multiple loop variables.
- Some people say that there should only be a single loop variable.
- What if there are multiple different types? Such as:
- Incrementing an int.
- Advancing an iterator.
And while loops are good at just repeating things and exiting the loop when a condition is met.
Sometimes, you’ll need to loop forever using an infinite loop.
- You can still have your code stop by breaking out of the loop.
A do while loop is another form of while loop you’ll learn about.
- These loops are not as common.
- They have an interesting application. Macros can use this type of loop to resemble a method that requires a semicolon.
Range based for loops are not just for cutting down on your typing. They express an intention to go through an entire collection.
At the end of the class, you’ll know what your options are and when to use each type of loop. This will let you write programs that respond better to user input. Imagine a word processor that made you tell it how many characters your document would have before you could start typing. It wouldn’t be very friendly, right? A loop lets your code deal with as much typing as the user wants before saving the document.