You Already Know How to Code: Part 6

In Part 5, I showed you how you already know about error handling. Here’s even more you already know about coding.

You Already Know About Control Flow

“If that’s true, I’ll jump for joy three times”

In life, how do we know what to do, in any given moment? Usually, we have certain information available to us. For example, we know the current weather outside. Given that, we can decide whether or not to take a coat when going out. What if we’re enjoying an activity, like playing a game of cards? We’d probably want to keep doing that until we get bored. What if something goes wrong while we’re out, like we miss the bus home? We have to figure out another way home. These are examples of using conditionals, loops and error handling – all of which we covered in the previous parts of this series. So what is Control Flow? Control Flow is simply putting all of these things together.

If we return to the skipping example used in Part 5, we’ll see that this is an example of a Control Flow. First, we start skipping and continue until we get tired or we trip. If we’re injured after a trip, we stop. At each point we are making a decision as to how to proceed given the current information available. That is Control Flow.

We can represent the control flow in a flow diagram like so:

This flow diagram represents the code in Part 5. You can use a flow diagram to illustrate how you want your code to behave in terms of decision points that determine where the code flows to next. The flow diagram represents the algorithm (the order of events), before you write any code. Of course, we learnt that we already knew about algorithms in Part 1. So Control Flow is a representation of how conditionals, loops and error handling will be used in the algorithm to determine the path the code will take for a given set of circumstances.

Conclusion

Control Flow is how you use control mechanisms like conditionals, loops and error handling to determine where the code will go for a given set of information or circumstances. You can create a Control Flow like a flow chart or algorithm without writing any actual code. The Control Flow is like the code design before you get into the implementation details. It’s useful to have an idea of your desired control flow before you start writing code.

If you want to learn more about coding, I suggest trying the free courses at CodeAcademy or SoloLearn. Thanks for reading, see you next time!