Control flow or flow of control is blocks of code we create to control the direction we take through the code. It’s common in programs to take a different direction based on some data. Think about a login screen. If the username and password are valid, we log them in. If not valid, we don’t. This is defined as flow control.
In very simple terms it’s the order we specify things to happen. The large majority of programming languages will run top down. Meaning they’ll start with line one of your code and then on to line 2 and so forth. Line one may take them to a whole new file, but the same principle applies- it will work it’s way down that file until eventually ending back at line one of the first file before progressing to line 2.
It’s important for us to get the sequence right, otherwise our program may not give us the result we intended.
The simplest way to add conditional flows into our code is to use if statements. All the major programming languages support ways to implement if statements.
A basic if statement will consist of two flows. The first flow is what the code should do if the condition is met and the second flow being what to do if the condition isn’t met. A simple example would be the login screen, if username and password are correct log the user in, else show them the invalid password screen.
The if statement functionality also gives us another option - elseif. Elseif allows use to go beyond true and false. So if were to introduce a third scenario into our logic example, we could take advantage of the elseif behaviour.
**IF** the username and password are correct and the account is verified, log the user in. **ElseIf** the username and password and correct but the account isn't verified, remind the user to verify their account. **Else** the username and password are incorrect, show the invalid password screen
We could add as many elseif statements as needed.
A simple flow diagram is a great way to help us model our if/else/elseif statements.
Iteration is where we do the same thing over the same block of code, sometimes also referred to as looping. A single instance of such logic would be called a loop.
The majority of the time we’ll be looping through a set of data or for a given period of time. For example, if we had a list of user accounts in our system, and wanted to apply a new value to all of them, we’d do so with a loop. A succinct way to say this would be “For every user in the system, run this block of code.” The code would keep executing until it has touched every user account. For is commonly the keyword used for this functionality in programming languages and such loops are sometimes referred to as a count-controlled loop.
Another loop you will commonly see is a loop based on a condition. Keep looping through this block of code until the condition is met. If we take the above example for updating users, let imagine we had a UI telling us the progress of our requests. The UI could have a condition loop in place that would show us the progress. While updating users is still in progress, show the spinner on the screen. Then the moment the updating users process is complete, our condition would be true (complete) and therefore we’d exit the loop and the UI would move onto a new screen. WHILE is commonly the keyword used for this functionality.
One of the best loops you will see, and hopefully create at some point, is an infinite loop. Ha, yes, they read that correctly - a loop that will never ever end. You don’t want to write one of these, but if you continue on your programming journey you eventually will. Creating an infinite loop is a kind of like a badge of honour for a programmer and you will know you have arrived when you create your first one.