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.

No comments:

Post a Comment