7.3. Combining Iterations and Conditionals

In this section, we will look at examples where: * We iterate inside of a conditional branch (inside an if, elif, or else block) * We use conditional execution inside an iteration block (loop)

Remember that we use conditionals structures when we need the computer to make a decision, and we use iteration when we need the computer to repeat things. So we combine these things when we need to repeatedly make decisions about something, or when the result of a decision means we need to do something repeatedly. We’ll look at each of these in turn.

7.3.1. Iteration inside Conditional Blocks

We have iteration inside of a conditional block when the result of a decision means we need to execute some code repeatedly. The iteration might occur in the if block, the else block or an elif block, or may occur in multiple places. Here’s what that could look like:

../_images/iteration_inside_conditional.png

The image above shows a few different versions of what it might look like to have a loop inside a branch of an if statement. There can be a loop in just one branch, in both or either branches of an if-else, and in any of the branches of an if-elif-else statement.

Let’s look at a simple example that encodes a user’s text input.

In the above example, we get user input. That should give us a string of letters. But, what if the user clicks cancel? In that case the input statement will return the special value “None”. We need to find out if the user gave us input or not. So we use an if statement to check. If the user_text isn’t “None”, then we want to go through each letter in the text and increment the character, so we use a loop in the if part of the if-else statement.

7.3.2. Conditional Execution inside Iteration Blocks

Conditional execution may happen inside of a for loop, and this is really common. It looks like this:

../_images/conditionals_inside_iteration.png

When we are iterating across a list of items, we often need to inspect each item and then make a decision about what to do with that item. Imagine we have a list of phrases and we want to separate single-word from multi-word phrases (items that contain a space). This flow chart shows what we want to achieve:

../_images/conditional_inside_iteration_flow_chart.png

The code below accomplishes this by using an if statement inside a for loop. We are also using the append method on lists, which we cover soon in the Sequences chapter.

Things to note abbout this code:

  • we are iterating through a list, and our iterator is called ‘greeting’, a singular of the list name ‘greetings’

  • for each greeting, we have a conditional statement that looks to see if there is a space in the item text, using the ‘in’ operator

  • this code would not work if the greetings list had non-text items in it (remember - it’s a bad idea to put different types in a list)

You have attempted of activities on this page