Definition “Control Flow” What is a control structure?
A control structure specifies in the program code in which order instructions are to be executed. Without special instructions, the programmed commands are executed sequentially, i.e. one after the other. Through control structures, alternative processes are also possible, e.g. jumps between different sections of the code or the execution of instructions depending on predefined conditions.
From jump command to control structure
Before there were modern control structures, the process of a program had to be precisely controlled in the source code by a jump command (e.g. Goto). In early programming languages, developers had to specify in the code exactly which line or which section of the code marked by a label should be jumped to next.
With a jump back, after the completion of the subroutine or the intermediate sequence, you finally got back to the main program. On the one hand, this gives a very detailed control over how a program runs, but on the other hand it is enormously complicated. Especially with complex programs you quickly lose the overview and the programming is uncomfortable.
Today, in contrast to this, you simply call a function, for example, or let a loop run through to the termination condition. After that, the program automatically jumps back to the appropriate place in the code, without the developer having to actively take care of these jumps.
Jump commands can still be used in many programming languages such as C / C++ if the programmer considers this useful or necessary. However, some modern programming languages such as Java or Python go so far that they no longer offer support for these commands. Here, the developer must realize the program flow completely through control structures.
Different types of control structures
Today, programmers have access to many types of control structures to comfortably control the flow of their programs. Without a special instruction, program code runs sequentially, that is, linearly in the order in which they were written down. Sometimes this is enough, for example, if you want to work through several subproblems one after the other. Through special control structures such as branches and loops, more complex processes can be easily realized.
Conditions and ramifications
Sometimes it is necessary that a program calls different functions depending on a condition or skips program parts. The simplest way to implement a conditional statement in the program code is the combination of if and else. If a previously defined condition x is true, the program should perform the action y – and if not, then it should do a different action z or cancel and return to the further program flow without doing anything.
For decisions with multiple paths, there are also multiple branches. Depending on requirements and technology, they can be implemented, for example, as a construct of if, then and else or as a switch case. Thus, the programmer can easily give instructions for each case, even if the condition can assume more states than just true or false, or if a combination of different conditions is to be checked.
Sometimes an instruction has to be repeated until a predetermined termination condition occurs or the condition for execution is no longer true. For this, various types of loops are available to the programmer. The loop executes the code defined in it and then automatically jumps back to the beginning of the statement to run the next iteration.
Different scenarios can be achieved with a loop:
- Counting Loop (For): The For loop counts towards a predefined upper limit with each iteration and aborts when this is reached. With each repetition, the trunk of the loop is performed. If you want to let something go through a predetermined number of iterations, for example, you can use this counting loop.
- Quantity Loop (Foreach): Some languages offer the foreach loop, which goes through all elements of a set. For example, the set can be a list or an array. It is a variant of the For loop, which makes a frequently occurring scenario a little more convenient for programmers. You can therefore also implement this type of loop with a for loop, e.g. if the programming language does not offer a ready-made quantity loop.
- Head-controlled loop (While – Do): With this loop, the condition is checked at the beginning, then the loop body is executed. The next iteration starts again with checking the condition. If the condition does not apply at the beginning, the loop will not run at all.
- Foot-controlled loop (Do – While): Here the condition is only queried after the body of the loop has been executed. So this loop will be run at least once in any case, because the condition is checked only after that.