- Explain the concept and purpose of iteration in programming
- Iterate over an array using the
Iteration in Real-World Scenarios
Iteration (or looping for a specified number of times) is the process of repeating a task for each object in a collection. For each of the scenarios below, we’ll walk through using scenarios we are familiar with to demonstrate the concept.
The following example would be programmatically challenging because it would require several steps, but is something a teacher may do every day:
A teacher is grading papers and takes the following steps:
- read through the paper
- mark the score on top
- record score in gradebook
- repeat steps 1-3 on next paper until stack is empty
What is another real world scenario where we repeat a task for each object in a collection?
Iteration in Programming
We could list tasks all day for the physical world - but what does this actually look like when writing software? Here are some examples:
- Sending a marketing email to each email address on the list
- Checking each inventory item to see if it includes the users search request
- Displaying a row in the table for each friend a user is connected with
As you may notice, each list must have a specific type of data in order for this to work. For the email example, if we had an array that held emails and phone numbers, that may create errors in the code that would stop it from running. That’s why it is a best practice to write arrays in a way that they hold data of the same data type and the same kinds of information!
Iteration can save us a lot of time and code. Consider our names arrays:
names = ["Kenny", "Marisol", "Ruben", "Hanna"] puts names puts names puts names puts names
If we wanted to take action with each name, we’d have to write that code out 4 times. If we added a name, or 30 names, we’d have to re-write that code for each name added. As you can imagine, this can become very redundant and create oportunities for human error!
There are many ways we can write code that iterates - some are better suited for certain situations, and sometimes the decision about which method to use will come down to the developer’s opinions and preferences.
Today, we will use the
.each method that is built into Ruby to iterate over arrays!
Let’s explore this example in a repl.it:
names = ["Kenny", "Marisol", "Ruben", "Hanna"] names.each do |name| puts name end
Try It: Iteration
Go back to your Favorite Foods project. If you've lost it or want to start with a fresh one, you can use this repl.it!
Instead of printing or puts-ing out the entire array as is, see if you can use
each to print a sentence for each item in the array or add an asterisk to serve as a bullet point. Here' one example:
What is your favorite breakfast food? hash browns What is your favorite lunch food? turkey sandwiches What is your favorite dinner food? angel hair pasta What is your favorite dessert? cheesecake ------------------------------ Great! Your favorite foods are: * hash browns * turkey sandwiches * angel hair pasta * cheesecake ...