Program Flow#

Midjourney: Doves to Blocks, ref. M. C. Escher

A fool with a tool is still a fool.

— Grady Booch

Slides/PDF#

The most important skill in programming is to translate a real-world problem into individual steps and communicate it. A tool for representing these steps is ‘Programmablaufpläne’, commonly also referred to as ‘Flussdiagramme’. These diagrams help to understand and optimize the structure and logic of the program.

Program flowcharts (flow diagrams)#

A tool for representing these steps is the program flowchart, commonly also referred to as a flowchart.

Flowchart example

They consist of individual elements connected by directed lines in the execution order. The shape of the elements defines their meaning.

They contain the basic building blocks of a program. These building blocks are:

  • Statements: They perform actions, such as outputting text or calculating values.

  • Variables: They store values that are used by the program.

  • Operators: They perform calculations or comparisons.

  • Branches: They enable making decisions and changing the program flow.

  • Loops: They repeat certain instructions as long as a condition is met.

  • Functions: They encapsulate code in reusable blocks.

There are start- and end-elements with rounded corners. They are the only ones with exactly one outgoing edge or exactly one incoming edge. They are sometimes omitted when the start and end are, for example, clearly recognizable by the reading direction.

Start-/End-Element

Instructions (Statements) are represented by square-bracketed elements. They can have multiple inputs but only one output. The text inside the element describes the instruction to be executed. The input and output variables can be written on the arrows.

Instruction

Inputs and outputs are represented by rhombuses. Here is the input Geld holen.

Input

if statements are represented by diamond shapes. They have at least two outputs. The condition is written inside the diamond, and the alternative values of the condition (usually Yes/No) are written on the outgoing arrows.

If statement

Subroutines, such as function calls, are depicted by square elements with double borders.

Subroutine

Function Flow#

Programs can be viewed as a sequence of numbered statements. They are executed sequentially (one after another) statement by statement, in the order defined in the program. When functions are called, the code defined inside the function is executed at the point of the call. The following example is intended to illustrate this. It defines a simple script that computes the Pythagoras’ theorem for two variables x and y with the help of a function. To illustrate the program flow, every line (except line 4, since this does not work in the syntax of the function definition) was augmented with a print statement for that line.

import math
x = 3; print("Zeile 1")
y = 4; print("Zeile 2")

def pythagoras(a, b):
  res = math.sqrt((a*2)+(b*2));  print("Zeile 5")
  return res;  print("Zeile 6");

p_xy = pythagoras(x, y);  print("Zeile 8")
print("Zeile 10")
Zeile 1
Zeile 2
Zeile 5
Zeile 8
Zeile 10

After running the program, we observe that lines 1 to 3 are executed in sequential order. Then the execution skips lines 5 and 6, because these define the function pythagoras, which has not yet been called. The function is only invoked at the start of line 8, which is why next line 5 is executed before line 8. The output for ‘Line 6’ is then missing, because the function has already terminated with the return. Since the function is called again with swapped parameters on line 9, line 5 is output again and then lines 9 and 10.

Thus we see that the program was executed sequentially from top to bottom. However, since the functions are repeatable statements, they are executed multiple times when called.

Quiz#

--- shuffleQuestions: true shuffleAnswers: true --- ### What is the main purpose of flowcharts? - [x] The translation of a real problem into individual program steps - [ ] Writing efficient code - [ ] Documentation of finished programs - [ ] Visualization of data structures ### What form do start and end elements in flowcharts take? - [x] Elements with rounded corners - [ ] Rectangular elements - [ ] Rhombuses - [ ] Circles ### What form do statements in flowcharts take? - [x] Rectangular elements - [ ] Rhombuses - [ ] Circles - [ ] Elements with rounded corners ### Sort the following elements of a flowchart by their typical order: 1. `Start (rounded rectangle)` 2. `Input (Rhombus)` 3. `Processing (Rectangle)` 4. `Decision (Diamond)` 5. `End (rounded rectangle)` ### What error is present in this flowchart? ```mermaid graph TD A[Start] --> B{Input} B --> C[Processing] C --> D{Decision} D --> B ``` - [x] Input is depicted with the wrong shape (Diamond instead of Parallelogram) - [ ] The arrow direction is wrong - [ ] Start element is missing - [ ] Decisions can not lead back to inputs ### What shape is used for if statements? - [x] Diamond - [ ] Rectangle - [ ] Rhombus - [ ] Circle ### What characterizes subroutine elements in flowcharts? - [x] Rectangular shape with a double line - [ ] Circular shape - [ ] Dashed lines - [ ] Shaded rectangles