2023-12-12 15:39:43 +01:00
|
|
|
# Flowchart examples
|
|
|
|
{:.no_toc}
|
|
|
|
|
|
|
|
<nav markdown="1" class="toc-class">
|
|
|
|
* TOC
|
|
|
|
{:toc}
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
## The goal
|
|
|
|
|
|
|
|
Looking into some flow chart examples.
|
|
|
|
|
|
|
|
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
|
|
|
|
|
2023-12-12 16:00:34 +01:00
|
|
|
## Most simple program
|
2023-12-12 15:39:43 +01:00
|
|
|
|
|
|
|
This program does nothing.
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:00:34 +01:00
|
|
|
start([Start])-->stop([Stop])
|
2023-12-12 15:39:43 +01:00
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
|
|
|
|
2023-12-12 16:27:03 +01:00
|
|
|
In Python:
|
|
|
|
|
|
|
|
```python
|
|
|
|
pass
|
|
|
|
```
|
|
|
|
|
2023-12-12 16:14:52 +01:00
|
|
|
## a+b=c
|
2023-12-12 16:00:34 +01:00
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:10:07 +01:00
|
|
|
start([Start]) --> inita{{"a ← 1"}} --> initb{{"b ← 1"}} --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop])
|
2023-12-12 16:00:34 +01:00
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
2023-12-12 15:39:43 +01:00
|
|
|
|
2023-12-12 16:10:07 +01:00
|
|
|
In Python:
|
2023-12-12 16:16:54 +01:00
|
|
|
|
2023-12-12 16:10:07 +01:00
|
|
|
```python
|
|
|
|
a=1
|
|
|
|
b=1
|
|
|
|
c=a+b
|
|
|
|
print(c)
|
|
|
|
```
|
2023-12-12 16:14:52 +01:00
|
|
|
|
|
|
|
## a+b=c with input from user
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
|
|
|
start([Start]) --> inputa[/"Input integer a"/] --> inputb[/"Input integer b"/] --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop])
|
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
|
|
|
|
|
|
|
In Python:
|
2023-12-12 16:16:54 +01:00
|
|
|
|
2023-12-12 16:14:52 +01:00
|
|
|
```python
|
|
|
|
a = int(input())
|
|
|
|
b = int(input())
|
|
|
|
c = a + b
|
|
|
|
print(c)
|
|
|
|
```
|
2023-12-12 16:27:03 +01:00
|
|
|
|
|
|
|
## for-loop / while loop
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:29:06 +01:00
|
|
|
start([Start]) --> initcounter{{"counter ← 0"}} --> initcountermax{{"counter_max ← 100"}} --> Condition{"counter < counter_max"}
|
2023-12-12 16:27:03 +01:00
|
|
|
Condition -- Yes --> printcounter[/"print counter"/] --> Action["counter ← counter + 1"]
|
|
|
|
Action --> Condition
|
2023-12-12 16:37:44 +01:00
|
|
|
Condition -- No --> stop([Stop])
|
2023-12-12 16:27:03 +01:00
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
|
|
|
|
|
|
|
In Python:
|
|
|
|
|
|
|
|
```python
|
|
|
|
counter_max = 100
|
|
|
|
for counter in range(0, counter_max):
|
|
|
|
print(counter)
|
|
|
|
```
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
```python
|
|
|
|
counter = 0
|
|
|
|
counter_max = 100
|
|
|
|
while counter < counter_max:
|
|
|
|
print(counter)
|
|
|
|
counter += 1
|
|
|
|
```
|
|
|
|
|
2023-12-12 16:37:44 +01:00
|
|
|
## if, elif, else
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
|
|
|
start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"}
|
2023-12-12 16:39:56 +01:00
|
|
|
Condition1 -- Yes --> Action1[/"print condition 1"/]
|
2023-12-12 16:37:44 +01:00
|
|
|
Condition1 -- No --> Condition2{"a == 2"}
|
2023-12-12 16:39:56 +01:00
|
|
|
Condition2 -- Yes --> Action2[/"print condition 2"/]
|
|
|
|
Condition2 -- No --> ElseAction[/"print condition else"/]
|
2023-12-12 16:37:44 +01:00
|
|
|
Action1 --> stop([Stop])
|
|
|
|
Action2 --> stop
|
|
|
|
ElseAction --> stop
|
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
|
|
|
|
2023-12-12 16:55:22 +01:00
|
|
|
In Python:
|
|
|
|
|
|
|
|
```python
|
|
|
|
a = int(input())
|
|
|
|
if a < 1:
|
|
|
|
print("condition 1")
|
|
|
|
elif a == 2:
|
|
|
|
print("condition 2")
|
|
|
|
else:
|
|
|
|
print("condition else")
|
|
|
|
```
|
|
|
|
|
|
|
|
## if, elif, else
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
|
|
|
start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"}
|
|
|
|
Condition1 -- Yes --> Action1[/"print condition 1"/]
|
|
|
|
Condition1 -- No --> Condition2{"a == 2"}
|
|
|
|
Condition2 -- Yes --> Action2[/"print condition 2"/]
|
|
|
|
Condition2 -- No --> ElseAction[/"print condition else"/]
|
|
|
|
Action1 --> stop([Stop])
|
|
|
|
Action2 --> stop
|
|
|
|
ElseAction --> stop
|
|
|
|
</pre>
|
|
|
|
{% endraw %}
|
|
|
|
|
|
|
|
In Python:
|
|
|
|
|
2023-12-12 16:37:44 +01:00
|
|
|
```python
|
|
|
|
a = int(input())
|
|
|
|
if a < 1:
|
|
|
|
print("condition 1")
|
|
|
|
elif a == 2:
|
|
|
|
print("condition 2")
|
|
|
|
else:
|
|
|
|
print("condition else")
|
|
|
|
```
|
|
|
|
|
2023-12-12 16:55:22 +01:00
|
|
|
## functions
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
|
|
|
Start([Start]) --> Input[/"Input integer a"/]
|
|
|
|
Input -- a < 1 --> Function1[["function_1()"]]
|
|
|
|
Function1 --> End([Stop])
|
|
|
|
Input -- a == 2 --> Function2[["function_2()"]] --> End
|
|
|
|
Input -- else --> FunctionElse[["function_else()"]] --> End
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:58:21 +01:00
|
|
|
Function1[["function_1()"]] --> Start([Start]) --> Print[/"print condition 1"/] --> End([Stop])
|
2023-12-12 16:55:22 +01:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:58:21 +01:00
|
|
|
Function2[["function_2()"]] --> Start([Start]) --> Print[/"print condition 2"/] --> End([Stop])
|
2023-12-12 16:55:22 +01:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<pre class="mermaid">
|
|
|
|
flowchart TD
|
2023-12-12 16:58:21 +01:00
|
|
|
FunctionElse[["function_else()"]] --> Start([Start]) --> Print[/"print condition else"/] --> End([Stop])
|
2023-12-12 16:55:22 +01:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
|
|
In Python:
|
|
|
|
|
|
|
|
```python
|
|
|
|
def function_1():
|
|
|
|
print("condition 1")
|
|
|
|
|
|
|
|
def function_2():
|
|
|
|
print("condition 2")
|
|
|
|
|
|
|
|
def function_else():
|
|
|
|
print("condition else")
|
|
|
|
|
|
|
|
a = int(input())
|
|
|
|
if a < 1:
|
|
|
|
function_1()
|
|
|
|
elif a == 2:
|
|
|
|
function_2()
|
|
|
|
else:
|
|
|
|
function_else()
|
|
|
|
```
|