Examples: Difference between revisions
From Master of Neuroscience Wiki
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 15: | Line 15: | ||
== a+b=c == | == a+b=c == | ||
{{#mermaid: | {{#mermaid: | ||
graph TD | |||
start([Start]) --> inita["a = 1"] --> initb["b = 1"] --> add["c = a+b"] --> printc[/"print c"/] --> stop([Stop]) | |||
}} | }} | ||
| Line 37: | Line 37: | ||
== for-loop / while loop == | == for-loop / while loop == | ||
{{#mermaid: | {{#mermaid: | ||
graph TD | |||
start([Start]) --> initcounter | start([Start]) --> initcounter["counter = 0"] --> initcountermax["counter_max = 100"] --> Condition{"counter < counter_max"} | ||
Condition -- Yes | Condition -->|Yes| printcounter[/print counter/] --> Action["counter = counter + 1"] | ||
Action --> Condition | Action --> Condition | ||
Condition -- No | Condition -->|No| stop([Stop]) | ||
}} | |||
In Python:<syntaxhighlight lang="python">counter_max = 100 | In Python:<syntaxhighlight lang="python">counter_max = 100 | ||
Latest revision as of 08:41, 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()