Examples: Difference between revisions
From Master of Neuroscience Wiki
Created page with "Looking into some flow chart examples. Questions to [mailto:davrot@uni-bremen.de David Rotermund] == Most simple program == This program does nothing. {% raw %}<pre class="mermaid"> flowchart TD start([Start])-->stop([Stop]) </pre>{% endraw %} In Python:<syntaxhighlight lang="python">pass</syntaxhighlight> == a+b=c == {% raw %}<pre class="mermaid"> flowchart TD start([Start]) --> inita{{"a ← 1"}} --> initb{{"b ← 1"}} --> add("c ← a+b") --..." |
No edit summary |
||
| Line 6: | Line 6: | ||
This program does nothing. | This program does nothing. | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start])-->stop([Stop]) | start([Start])-->stop([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">pass</syntaxhighlight> | In Python:<syntaxhighlight lang="python">pass</syntaxhighlight> | ||
== a+b=c == | == a+b=c == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start]) --> inita{{"a ← 1"}} --> initb{{"b ← 1"}} --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop]) | start([Start]) --> inita{{"a ← 1"}} --> initb{{"b ← 1"}} --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">a=1 | In Python:<syntaxhighlight lang="python">a=1 | ||
| Line 25: | Line 25: | ||
== a+b=c with input from user == | == a+b=c with input from user == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start]) --> inputa[/"Input integer a"/] --> inputb[/"Input integer b"/] --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop]) | start([Start]) --> inputa[/"Input integer a"/] --> inputb[/"Input integer b"/] --> add("c ← a+b") --> printc[/"print c"/] -->stop([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">a = int(input()) | In Python:<syntaxhighlight lang="python">a = int(input()) | ||
| Line 36: | Line 36: | ||
== for-loop / while loop == | == for-loop / while loop == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start]) --> initcounter{{"counter ← 0"}} --> initcountermax{{"counter_max ← 100"}} --> Condition{"counter < counter_max"} | start([Start]) --> initcounter{{"counter ← 0"}} --> initcountermax{{"counter_max ← 100"}} --> Condition{"counter < counter_max"} | ||
| Line 42: | Line 42: | ||
Action --> Condition | Action --> Condition | ||
Condition -- No --> stop([Stop]) | Condition -- No --> stop([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">counter_max = 100 | In Python:<syntaxhighlight lang="python">counter_max = 100 | ||
| Line 53: | Line 53: | ||
== if, elif, else == | == if, elif, else == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"} | start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"} | ||
| Line 63: | Line 63: | ||
Action2 --> stop | Action2 --> stop | ||
ElseAction --> stop | ElseAction --> stop | ||
}} | |||
In Python:<syntaxhighlight lang="python">a = int(input()) | In Python:<syntaxhighlight lang="python">a = int(input()) | ||
| Line 74: | Line 74: | ||
== if, elif, else == | == if, elif, else == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"} | start([Start]) --> inputa[/"Input integer a"/] --> Condition1{"a < 1"} | ||
| Line 84: | Line 84: | ||
Action2 --> stop | Action2 --> stop | ||
ElseAction --> stop | ElseAction --> stop | ||
}} | |||
In Python:<syntaxhighlight lang="python">a = int(input()) | In Python:<syntaxhighlight lang="python">a = int(input()) | ||
| Line 95: | Line 95: | ||
== functions == | == functions == | ||
{ | {{#mermaid: | ||
flowchart TD | flowchart TD | ||
Start([Start]) --> Input[/"Input integer a"/] | Start([Start]) --> Input[/"Input integer a"/] | ||
| Line 102: | Line 102: | ||
Input -- a == 2 --> Function2[["function_2()"]] --> End | Input -- a == 2 --> Function2[["function_2()"]] --> End | ||
Input -- else --> FunctionElse[["function_else()"]] --> End | Input -- else --> FunctionElse[["function_else()"]] --> End | ||
}} | |||
{{#mermaid: | |||
flowchart TD | flowchart TD | ||
Function1[["function_1()"]] --> Start([Start]) --> Print[/"print condition 1"/] --> End([Stop]) | Function1[["function_1()"]] --> Start([Start]) --> Print[/"print condition 1"/] --> End([Stop]) | ||
}} | |||
{{#mermaid: | |||
flowchart TD | flowchart TD | ||
Function2[["function_2()"]] --> Start([Start]) --> Print[/"print condition 2"/] --> End([Stop]) | Function2[["function_2()"]] --> Start([Start]) --> Print[/"print condition 2"/] --> End([Stop]) | ||
}} | |||
{{#mermaid: | |||
flowchart TD | flowchart TD | ||
FunctionElse[["function_else()"]] --> Start([Start]) --> Print[/"print condition else"/] --> End([Stop]) | FunctionElse[["function_else()"]] --> Start([Start]) --> Print[/"print condition else"/] --> End([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">def function_1(): | In Python:<syntaxhighlight lang="python">def function_1(): | ||
Revision as of 08:36, 21 October 2025
Looking into some flow chart examples.
Questions to David Rotermund
Most simple program
This program does nothing.
In Python:
pass
a+b=c
In Python:
a=1
b=1
c=a+b
print(c)
a+b=c with input from user
In Python:
a = int(input())
b = int(input())
c = a + b
print(c)
for-loop / while loop
In Python:
counter_max = 100
for counter in range(0, counter_max):
print(counter)
or
counter = 0
counter_max = 100
while counter < counter_max:
print(counter)
counter += 1
if, elif, else
In Python:
a = int(input())
if a < 1:
print("condition 1")
elif a == 2:
print("condition 2")
else:
print("condition else")
if, elif, else
In Python:
a = int(input())
if a < 1:
print("condition 1")
elif a == 2:
print("condition 2")
else:
print("condition else")
functions
In 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()