Get startedGet started for free

Control flow in procedural programming

1. Control flow in procedural programming

Welcome to this video on control flow in procedural programming! We're going to be discussing what control flow is, some examples of control flow, and how it relates to the procedural programming paradigm. Let's jump in.

2. What is control flow?

One of the defining features of procedural programming relative to other types of imperative programming is how it handles control flow of the program. Control flow is the set of keywords and processes within a programming language that indicate how the logic of the code should be stepped through. Some common examples of control flow mechanisms in Python include: if/else statements, for loops and while loops, and function definition. Recall that if/else statements are used to check if a particular statement is true or false. For loops and while loops are used to iterate through a set of options. Function definition is used to write a piece of reusable code that can be referenced later. All of these will come into play in procedural programming.

3. Combining elements of control flow (if statements and for loops)

The power of control flow in procedural programming comes in the combination of different control flow mechanisms. For instance, in this example, we use a simple if statement to determine who is taller. In this second example, we are using a for loop to go through a list of heights and print out each of those heights. By combining both an if statement and a for loop in this third example, we are able to compare one specific height to all of the heights in the group and print out a message based on that comparison. To do this, we first loop through the list of heights, and then within the for loop, we use an if statement for the comparison.

4. Combining elements of control flow in functions

Besides combining if statements and for loops, we can go even further by putting this combination of control flow elements into a function. By defining a Python function with all of the logic we need in it, we are taking advantage of the concept of the "procedure" in procedural programming. Notice that we have copied the logic for comparing one height to a list of heights into a function called compare_heights. After defining this function, we can define some variables to pass into it, and then call the function with those values. Now we have a reusable procedure to compare heights!

5. Control flow in procedural programming

Bringing this all together, this ability to direct the flow of code execution through the program using control flow is what makes procedural programming in Python possible. If we can structure our program logic in a step-by-step way with clearly separated chunks of reusable logic, we can code it in Python using if statements, loops, function definition, and other control flow elements. Recall that procedures are how this paradigm implements separation of responsibilities, and control flow is how we build those procedures. Now we have everything we need to be able to write procedural programs in Python!

6. Let's practice!

Awesome! Now we have all of the building blocks we need to implement procedural programming in Python. Let's solidify some concepts and then construct our first procedural program!