So far, we have said that a program is a sequence of statements (or actions) that execute linearly from start to finish.
Well… actually that is not entirely true. It is true that our program will become a series of instructions, placed one after the other.
When executed, somewhere, the computer will keep track of what the current instruction is. This “arrow” indicating the instruction to execute will change to traverse the entire program.
However, if the execution point could only move forward one step at a time, we could only perform a predetermined sequence of actions (we could do “heavy calculations” but little else).
For this to be interesting, the execution point can change at any moment to any other point in the program (that is, it can jump several instructions forward, and even backward).
This is what we call the program flow of execution. The instructions and tools we have to manage and change the program are called control flow structures.
Control flow structures are instructions that allow a program to make decisions and perform different actions based on given conditions, and they are one of the most important parts of programming.
Types of control flow structures
There are several types of control flow structures, including:
- Jump structures: allow the programmer to transfer control flow to a different location in the program. The most common jump structure is 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 multiple times. The most common loop structures are WHILE, DO-WHILE, FOR, FOREACH.
- Error handling structures: allow the program to respond to errors or exceptions. The most common is TRY-CATCH.
Not all of them are viewed equally “well”. In particular, we will see that GO-TO is something we should not use, and TRY-CATCH would be a topic for debate. But since the goal of the course is to talk about programming and its evolution, I think it is important to include all of them.