So far we had said that a program is a sequence of statements (or actions) that are executed linearly from beginning to end.
Well… actually that is not entirely true. It is true that our program will become a series of instructions, placed in a series one after the other.
When executed, the computer will keep track of which is the current instruction. This “arrow” that indicates the instruction to be executed, will change to go through the entire program.
However, if the execution point could only move one by one forward, we could only do a predetermined sequence of actions. We could do “big calculations” but little else.
For this to be interesting the execution point can change at any time to any other point in the program. That is, it can jump several instructions forward, and even backwards.
This is what we call the program’s execution flow. The instructions and tools we have to manage and change the program, we call flow control structures.
Flow control structures are the instructions that allow a program to make decisions and perform different actions according to the given conditions, and they are one of the most important parts of programming.
Types of flow control structures
There are several types of flow control structures, including:
- Jump structures: allow the programmer to transfer flow control to a different location in the program. The most common jump structure is the GO-TO.
- Conditional structures: allow the program to make decisions based on a condition. The most common conditional structures are IF, IF-ELSE, IF-ELSEIF SWITCH, and the ternary operator.
- Loop structures: allow the program to repeat a sequence of instructions several times. The most common loop structures are WHILE, DO-WHILE, FOR, FOREACH.
- Error management structures: allow the program to respond to errors or exceptions. The most common one is TRY-CATCH.
Not all of them are equally “well seen”. In particular, we will see that GO-TO is something we shouldn’t use, and TRY-CATCH would be up for debate. But as the goal of the course is to talk about programming and its evolution, I think it is important to include all of them.