7.5. Combining Iterations, Conditionals and Functions

In this section, we will look at examples where we combine all three of these building blocks together in various ways. The purpose of looking at these examples, along with the flow charts that are color-coded (blue to show function blocks, green to show conditional blocks, and pink to show iteration blocks) is the ways that these things can all work together. Every program you write will make use of these blocks of code, nested in each other in lots of different ways.

Let’s look at an example of a program that uses all of these components, combined and nested together. This program is designed to check through a list of potential passwords and determine whether each password meets a set of four requirements. Run the program. This program is not interactive, so the output is only in the console. But note the use of functions, loops, conditionals and how they are combined.

After you have run the program above, look at the code image below, which highlights with the functions, conditionals and loops with the same color-coding we’ve been using. We’ve also added arrows from the function calls to the function blocks.


Here are some things to pay attention to in the image above:

You have attempted of activities on this page