# Algorithm flowchart

> A loop that sums the numbers 1 to n: terminals, input and output, a process, and a decision. A Flowcharts example with its LaTeX source.

A Flowcharts example in the TeXeR TikZ library: https://texer.org/library/flowcharts/algorithm-flowchart

Package: [Flowcharts](https://texer.org/library/flowcharts.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: flowchart, algorithm, loop, decision

## Source

```latex
\usetikzlibrary{shapes.geometric, shapes.misc, arrows.meta, positioning}

\begin{tikzpicture}[
  node distance=6mm and 12mm,
  every node/.style={font=\small},
  terminal/.style={rounded rectangle, draw, minimum width=20mm, minimum height=7mm},
  process/.style={rectangle, draw, minimum width=26mm, minimum height=7mm},
  io/.style={trapezium, trapezium stretches body, trapezium left angle=70, trapezium right angle=110, draw, minimum width=22mm, minimum height=7mm},
  decision/.style={diamond, aspect=2, draw, inner sep=1pt},
  arrow/.style={-{Stealth}, thick},
]
  \node[terminal] (start) {Start};
  \node[io, below=of start] (read) {Read $n$};
  \node[process, below=of read] (init) {$i \gets 1$, $s \gets 0$};
  \node[decision, below=of init] (test) {$i \le n$?};
  \node[process, right=of test] (add) {$s \gets s + i$, $i \gets i + 1$};
  \node[io, below=of test] (print) {Print $s$};
  \node[terminal, below=of print] (stop) {Stop};

  \draw[arrow] (start) -- (read);
  \draw[arrow] (read) -- (init);
  \draw[arrow] (init) -- (test);
  \draw[arrow] (test) -- node[above] {yes} (add);
  \draw[arrow] (add) |- (init);
  \draw[arrow] (test) -- node[right] {no} (print);
  \draw[arrow] (print) -- (stop);
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{shapes.geometric, shapes.misc, arrows.meta, positioning}

\begin{document}

\begin{tikzpicture}[
  node distance=6mm and 12mm,
  every node/.style={font=\small},
  terminal/.style={rounded rectangle, draw, minimum width=20mm, minimum height=7mm},
  process/.style={rectangle, draw, minimum width=26mm, minimum height=7mm},
  io/.style={trapezium, trapezium stretches body, trapezium left angle=70, trapezium right angle=110, draw, minimum width=22mm, minimum height=7mm},
  decision/.style={diamond, aspect=2, draw, inner sep=1pt},
  arrow/.style={-{Stealth}, thick},
]
  \node[terminal] (start) {Start};
  \node[io, below=of start] (read) {Read $n$};
  \node[process, below=of read] (init) {$i \gets 1$, $s \gets 0$};
  \node[decision, below=of init] (test) {$i \le n$?};
  \node[process, right=of test] (add) {$s \gets s + i$, $i \gets i + 1$};
  \node[io, below=of test] (print) {Print $s$};
  \node[terminal, below=of print] (stop) {Stop};

  \draw[arrow] (start) -- (read);
  \draw[arrow] (read) -- (init);
  \draw[arrow] (init) -- (test);
  \draw[arrow] (test) -- node[above] {yes} (add);
  \draw[arrow] (add) |- (init);
  \draw[arrow] (test) -- node[right] {no} (print);
  \draw[arrow] (print) -- (stop);
\end{tikzpicture}
\end{document}
```
