Showing posts with label while loops. Show all posts
Showing posts with label while loops. Show all posts

Thursday, August 4, 2016

Steven Matz, While Loops, & Control Transfer Statements

There's a phone in the dugout for a reason. When the starting pitcher gets in trouble, the manager or pitching coach can pick up the phone and make a call to the bullpen for a reliever or two to start warming up.



Usually, there are a few different reasons for the bullpen to get up. The starter is hurt and needs to come out immediately. The starter is getting shelled and only has a batter or two left in the tank. Or the starter is approaching a pitch count limit.

In our last post we discussed while loops and used Dellin Betances' success against Mike Napoli as an example. In that example, we told Betances to keep pitching Napoli the same way until Napoli's OPS versus him improved to .500.

In this case, though, we have a few possible scenarios to consider.  So let's pretend the Mets' Steven Matz is on the bump and that he just threw his 75th pitch and that he has a 100 pitch limit.



Now let's run through the most dire situation first, that it's true that Matz is injured and needs to come out of the game immediately. Why should we address this first? Because if he has to come out there is no need to go through the rest of the loop up to 100 pitches. He is done now. Here's what that looks like:



So how do we know that the loop won't continue if it is true that Matz is injured? That's what the "break" line does: It exits the loop by breaking out of it.

Now let's go to the second most dire situation, that it's true that Matz is getting shelled. Here's what that looks like:



We use break again here because once we take Matz out of the game there is no need to keep running the loop up to 100 pitches.

And what if Matz is reaching his 100 pitch limit? Let's take a look at what that looks like:



In this case, it is also true that Matz is not getting shelled so as he approaches his 100 pitch limit his coach, Terry Collins, can say, "Good pitchin', kid!" with every pitch, but as soon as he reaches 90 pitches Collins makes the call to get righty Addison Reed and lefty Jerry Blevins up in the 'pen assuming it's a tight game.

This whole rigamarole is called a control transfer statement which just rolls off the tongue, doesn't? That name is about as clear as mud, right? Right. Alas, not all ballplayers are named Ruth and Mays - easy to remember. We have some Yastrzemskis and Rzepczynskis too.

Here's how I like to remember it: control transfer statements help me transfer control of the function depending on the case. In Matz's case, we transferred control to the "it is true Matz is injured" statement when that Boolean was true. We transferred control to the "it is true Matz is getting shelled" part of the function when that was true.

Why are control transfer statements important?
As the Big Nerd Ranch Guide to Swift Programming explains, "Control transfer statements allow you to modify the order of execution in some control flow. These statements transfer control from one chunk of code to another." The Big Nerds go on to say, "In the context of a loop, you can control whether execution iterates to the top of the loop or leaves the loop altogether."

How do we use control transfer statements?
In the Matz example, we used "break" to control the order, or, more specifically, to break out of the loop and end it altogether. If we wanted to control the order in a different way we could use "continue" in the same way we used "break." What would this do? Instead of ending the loop, it would restart the loop at the top and would skip over everything that comes after the "continue" command.

In the Matz example, if we added a "continue" after the final bracket ("}") of the matzGettingShelled portion, it would go back to the beginning and run on an infinite loop. Why? Since the function never reaches its bottom it fails to add pitches to Matz' pitch count and just keeps checking whether he is hurt or if he is getting shelled. Infinitely.

Break and continue: pretty powerful stuff.

Challenge: Create a while loop with control transfer statements for your favorite pitcher, manager, and bullpen and then tweet a screenshot of it to me (@randallmardus).

On deck: Type conversions.

Friday, July 22, 2016

Dellin Betances, Bugs Bunny, and While Loops

If a "for loop" is ideal for going through a batting order, a "while loop" is good for pitching strategy, namely because as a pitcher you go with a certain plan until something bad happens.

For example, let's say Dellin Betances is facing Mike Napoli. To date, Napoli is 0-7 off Betances with four Ks and three walks. Whatever Betances is doing so far, is working, but those three walks also show that he's being careful and hoping Napoli chases bad pitches, namely his curve/slider/knuckle-curve/Bugs Bunny pitch low-and-away.



In short, a while loops says, "While X is true, do Y." In Betances' case against Napoli that translates to, "While I continue to strike him out, continue to throw him nasty Bugs Bunny slurves low-and-away."



Here's a more specific example. Let's say that while Betances continues to give up less than two dingers to Napoli (to this date, no one has hit more than one off of Betances), Betances should continue to pitch him the same way.



Let's talk through this one. We have a variable for the number of homers Napoli has hit off of Betances (napoliHRsVsBetances) which is currently set to zero.

Then we have our while loop. While the number of Napoli homers versus Betances is less than two we will continue the loop and Betances will, "Keep the same game plan." The first time through the loop napoliHRsVsBetances is zero so we tell Dellin to keep the same game plan. We then add one to the napoliHRsVsBetances variable and restart the loop.

The second time through the loop the one homer by Napoli off of Beteances we have is still less than our cutoff of two, so Dellin can stay with the game plan. Because we are still less than our cutoff of two homers, we use the operator shorthand (++) to add one more to the number of Napoli homers versus Betances which is now up to two.

Now what happens? Now that napoliHRsVsBetances = 2 and our cutoff is two, the loop stops running. Why? Because it is no longer true that Napoli's number of homers off of Betances is less than two. At this point it is equal to two. So now, according to our loop, Betances should change up his approach to Napoli.

Challenge: Create a while loop for your favorite pitcher and his hitting nemesis and tweet it to me (@randallmardus).

On Deck: While Loops & Control Transfer Statements