# Switching and Nesting

## Switching and Nesting... or ELSE!

Our conditional blocks up to now have been been analogous to light switches—when turned “on,” something occurs, but when turned “off,” that something does not occur. This makes sense given the nature of our conditions—they are binary—either on or off.

However, this is not the only kind of binary switch we can model in a program. Consider a railroad switch as a type of binary, either/or switch.

With this type of switch, rather than choosing to do something or not, you are selecting between two alternate outcomes. One (and only one) of the outcomes will occur! In the railroad switch example, the train, upon approaching the junction, will take either the left or the right track, depending on how the switch is oriented.

Scratch allows for this type of conditional with the `if / else` block: Check the condition. Is it true or false?

If it is true, then do what is enclosed in the `if` portion of the block. This is exactly the same as the standard `if` block.

If it is false, then do what is enclosed in the `else` portion of the block. This differs from the standard if block. Instead of doing nothing rather than what is enclosed in the `if` block, an alternate set of blocks is executed.

## Russian Nesting Dolls

“If you can’t fly then run, if you can’t run then walk, if you can’t walk then crawl, but whatever you do you have to keep moving forward.” —Martin Luther King, Jr.

The Russian nesting dolls are an iconic example of Russian culture. However, they also illustrate an important construct in computer programming—nested blocks.

## Motivation

Our newly learned `if / else` block is powerful; it allows a program to follow one of two paths upon reaching a condition. In other words, it allows the program to make an either/or decision.

Example:

`````` Are you thirsty?

Options: [YES]  [NO]
``````

However, this is not how humans decide most things. Generally, there are more than two possible outcomes to a decision.

Example:

`````` What would you like to drink?

Options: [Water]  [Soda]  [Coffee]  [Juice]
``````

It would be nice if our programs could choose from among multiple different paths rather than just two.

## A Precursor to the Next Unit

We will examine this concept in much greater detail in Unit 3: Data Representation, but for now we will rely on our intuitions. Although each bit (in this case, each condition) represents a dichotomy between two possible choices, they can be chained together to represent more. Let’s look at how we might “chain” `if / else` blocks together to represent the questions/answers above.

The first question: “Are you thirsty?”

Notice that if the `answer` is yes, the program asks “What would you like to drink?” If the `answer` is no, the program simply continues on. The second question is dependent on the the `answer` of the first.

The second question: “What would you like to drink?”

There are four possible answers, so the program handles each to produce four different outputs. However, notice that the `if / else` blocks are nested, much like nesting dolls, where one `if / else` block is contained entirely within another. This is different than saying, “If it’s water, do this. If it’s soda, do that. If it’s coffee, do x, and if it’s juice, do y.”

Like the nesting dolls, the blocks within an `if` or `else` portion of a block are not even accessed/executed unless the program “uncovers” them by taking the appropriate branch. So, in our example program, the `answer` is not even checked against soda if the `answer` was already matched to water. At the point at which the program finds a match for `answer`, it is effectively done searching for one.

The algorithm is as follows:

• Check to see if the answer is water.
• If it is, output “Drink 8 glasses a day!” [DONE]
• If it is not, check to see if the answer is soda.
• If it is, output “Bad choice! That stuff is full of sugar.” [DONE]
• If it is not, check to see if the answer is coffee.
• If it is, output “Too much caffeine!” [DONE]
• If it is not, output “Juice is delicious.” [DONE]

If [DONE] is ever reached, then the program is effectively done with executing the nested `if / else` blocks.

## Discussion Questions:

The blocks tying both questions together:

How is juice matched if it is never explicitly checked for? How many times is `answer` checked in each of the following cases?

• no
• yes, water
• yes, soda
• yes, coffee
• yes, juice

How could the program be modified to check the answers to ensure that they are valid. In other words, if someone answers Sunny D or milk to “What would you like to drink?” the response would be “Sorry, we don’t have that.”