Wednesday, July 20, 2016

For Loops Through the Batting Order

What are the best baseball lineups of all-time?

How about the 1927 Murderers' Row Yankees? That lineup included:

Combs
Koenig
Ruth
Gehrig
Meusel
Lazzeri
Dugan
Collins
(Pitcher)


What about the 1004 run 1930 Cardinals? That lineup had:

Douthit
Adams
Frisch
Bottomley
Hafey
Watkins
Mancuso
Gelbert
(Pitcher)




Or how about the 1027 run 1950 Boston Red Sox lineup that featured these guys:

DiMaggio
Pesky
Goodman
Stephens
Dropo
Doerr
Zarilla
Tebbetts
(Pitcher)

Who knows? They all scored a lot of runs and turned over the batting order time and time again.

And like a batting order of nine guys that starts back at the top after the ninth hitter hits, so a loop in coding does the same.

Let's look at a generic batting order, 1-9:


What's going on here? This is what's called a "for loop". As the Big Nerd Ranch Guide to Swift Programming explains, "The for loop is ideal for iterating over the specific elements of an instance or collection of instances when the number of iterations to perform is either known or easy to derive."

Why are for loops important?
For loops are particularly important if you have a large database of items that you need to add to, subtract from, multiply from, or divide by a consistent number throughout the database.

When do we use for loops?
If you had a small database of a few figures that you needed to add one to, you could do it yourself, but if your database is all current MLB players, that would take you hours. A for loop helps you automate the process and save a lot of time.

Back to the book's definition, which is nice, but what does it mean? Let's take that batting order example above. As we know, a batting order has nine spots in the lineup. For now, we've set our variable battingOrder to zero. Why? Because we want to establish that battingOrder is an integer and, because it is a variable, we know it is probably going to change so by setting it to zero we don't set our hearts on it.

What about that "for b in 1...9"? Good question. In this case, I chose b as in batter so I can remember it better, but you could use any letter. Why? Because it is just a placeholder. And because this is our first time through this loop we can read this as, "For every batter in lineup spot one through nine..."

On to the next line. As we learned in the last post, the ++battingOrder is operator shorthand and another way of saying, "Add one to the current value of the battingOrder variable." In this case, that means, add one to zero which gives us one.

The last line is a command to print out the current value of the battingOrder variable. At this point, it will print out 1.

And then, because it is a loop, it will start over again, but this time it will start with battingOrder = 1 and will end up printing out 2 and so forth.

So when does our for loop stop?

This for loop will stop at nine because we told it in the first line of the loop that our parameters are 1...9. If you scroll over the white circle in your Xcode Playground to the far right of the ++battingOrder line you will see this:



While this image may not remind you of a batting order, what you can see is that we added one each time through the loop starting at one (the leadoff hitter) to nine, the last hitter.

And the "(9 times)"? What is that? That's the number of times we went through the loop.

Challenge: Write a for loop that simulates an intentional walk and tweet a screenshot of your code to me (@randallmardus).

On Deck: While Loops


No comments:

Post a Comment