---
# System prepended metadata

title: Flowcharts

---

# Flowcharts

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/rJjfXqS08) ... [next](https://hackmd.io/@alexhkurz/H1CyS5v08) ... or skip the excursions and continue with [global variables](https://hackmd.io/@alexhkurz/Hkc7HoSC8))

Flowcharts  are a great way to visualise algorithms. In particular, the control flow in imperative programs based on conditionals and loops can be made transparent.
- Look at the examples in [Wikipedia](https://en.wikipedia.org/wiki/Flowchart).

-  Read the short fascinating [history](https://en.wikipedia.org/wiki/Flowchart#History) of flowcharts.

    In this session we will learn another reason why flowcharts fell out of fashion after the 1970ies. When I was a computer science student around 1990, flowcharts played no role in our curriculum. (On the other hand, I remember that shortly afterwards, [UML](https://en.wikipedia.org/wiki/Unified_Modeling_Language) became a hot topic in the research community in which I did my PhD.) Anyway, one reason why flowcharts went out fashion in computer science for a while is that they do not deal well with recursion.

**Activity:** Take pen and paper and write flowcharts for the programs you have seen, or wrote yourself, in the session on [`for` loops](https://hackmd.io/@alexhkurz/H1o4Mcr6L). For example for

	sum = 0
	for i in range(0,101):
	    sum = sum + i
	print(sum)

and

    n = 20
	fib_one = 0
	fib_two = 1
	for i in range(0,n+1):
	    if i == 0:
	        fib = fib_one
	    elif i == 1:
	        fib = fib_two
	    else:
	        fib = fib_one + fib_two
	        fib_one = fib_two
	        fib_two = fib
    print(fib)


- The programs above are executed line by line, time flowing downwards.
- This "order of operations" (aka control flow) is modified by conditionals (`if-then-else`) and loops. 
-  Can you trace the control flow by going with your finger through the program as you execute it in your mind?
- Did you notice that both conditionals and loops made you jump around in the code?
-  The flowchart makes this control flow explicit. In the flowchart you can execute the program by just following the arrows.

You may have noticed that the examples above do not contain functions nor recursion. Functions can be added to flowcharts easiy, but with recursion flowcharts get quite a bit more complicated.

But notice that what makes recursion a great problem solving technique is essentially the same reason that makes recursion more difficult to understand from an operational point of view. Recursion is easier from a logical and mathematical point of view because it abstracts from time. 

So it does not come as a surprise that for recursion the "follow the program with your finger" method does not work in an obvious way. In fact, to do recursion in a flowchart style, with conditionals and loops, you have to dynamically change the program/flowchart by using a "call stack", see the [computation of fib 6](https://hackmd.io/@alexhkurz/HJiulVg0U#Computing-fib6-using-a-Call-Stack) for an example of how to use a call stack.



## References

[How Recursion Works — Explained with Flowcharts and a Video](https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/)
