Control Flow

Computers can perform instructions on their own, but they can’t make decisions on their own.

Control flow is the order of execution in a program.

We use control flow to help guide computers to make the decisions that we want.

The concept of control flow answers the question: “how do computers decide how to perform a series of actions, and in what order?”

For example, take a program that lets you browse the web.

To browse the web, you might go through the following steps – Start, Open Browser, Load Text, Load Images, End.

This program is a set of steps (or instructions) that is performed in a certain order. This order is called Control Flow.

In its basic form, control flow works in a way similar to a flow chart, starting from the top all the way to the end.

Therefore, we see that the concept of control flow is very vital, if we want computers to do what we tell them to do, and do it how we want it.

So, how do we use control flow to guide the computers?

Answer: By using control structures.

That’s right – we use control structures to adjust our control flow.

Specifically, we use control structures to tell a computer which instructions to execute, and in what order.

There are three main control structures:

  1. Conditionals. “Do X if Y is true”
  2. Loops. “Do X multiple times”
  3. Exceptions. “Do instruction set X, but if an error occurs, switch to instruction set Y”

The 3 Main Control Structures

1. Conditional Control Structures (or Conditionals)

Here, the computer’s instruction depends on some condition(s). That is, if some condition is met, then do X. Otherwise, do Y.

For example, when trying to login to an online account and you need to type in the password.

  1. Check password accuracy
  2. If true, accept and login
  3. If false, return “password incorrect”

The control flow is 1-2 if the password is right and 1-3 if the password is wrong. The condition is the accuracy of the password.

We use conditionals to write programs that do various things in various scenarios.

So, for our password example, we can have two control flows.

  1. Start – Is the password accurate? – If true, accept password and login – End
  2. Start – Is the password accurate? – If false, return “password incorrect” – End

2. Loops

We use loops so computers can help us out with repetitive tasks.

That is, do X number of times; or do X repeatedly until some condition is met.

A loop is a set of instructions which is stated once but which may be performed multiple times in a row.

For example, write letter “A” fifteen times. The instruction is to write letter “A”, and the loop does it fifteen times.

We can also use loop to carry out a set of instructions until a certain condition is met. For example, write letter “A” until you fill two pages of text.

Another example:

Codecademy’s loop for playing sounds (Source).

The condition that must be met to stop the loop is if the loop has ran four times.

3. Exceptions

We can make room for exceptions or alternatives in programming.

That is, do steps X, Y, Z. If an error happens, stop, and do steps A, B, C.

To work with exceptions, or errors, in programming, we account for them.

We can define two sets of instructions. The first set of instructions is performed, and if an exception or error happens, the first set is stopped and the second set is performed.

Resources: 1

2 thoughts on “Control Flow”

Leave a Comment