Algorithm: sum of 1 to N (loop) flowchart

This is the simplest way to show a loop in a flowchart. The decision checks the counter, the "yes" branch does the work and jumps back, and the "no" branch leaves the loop. It maps directly to a for or while loop in code.

How to write steps
  • One step per line. Lines connect top to bottom.
  • End a line with ? to make a decision. Indent the answers under it: yes: Ship it, no: Refund.
  • Indent more lines under an answer to continue that branch.
  • -> Step name jumps back to an earlier step (a loop). no -> Step name works inside a decision.
  • Start, End, Done or Stop become rounded start/end shapes.
  • Shape tags: [input] [output] [doc] [db] [sub] [manual] [delay].
  • lane: Sales on its own line puts the next steps in a swimlane.
  • Or paste Mermaid code starting with flowchart TD. It’s detected automatically.

Mermaid code for this chart
flowchart TD
  n1(["Start"])
  n2[/"Read N"/]
  n3["Set sum to 0 and i to 1"]
  n4{"Is i less than or equal to N?"}
  n5["Add i to sum"]
  n6["Add 1 to i"]
  n7[/"Print sum"/]
  n8(["End"])
  n1 --> n2
  n2 --> n3
  n3 --> n4
  n4 -->|"yes"| n5
  n5 --> n6
  n4 -->|"no"| n7
  n7 --> n8
  n6 --> n4

The steps as text

This is the source of the chart above, so the template is readable even without the diagram.

Start
[input] Read N
Set sum to 0 and i to 1
Is i less than or equal to N?
  yes: Add i to sum
    Add 1 to i
    -> Is i less than or equal to N?
  no: [output] Print sum
End

More flowchart examples

Questions people ask

How do I edit this algorithm: sum of 1 to n (loop) flowchart?

Change the text in the editor. Each line is a step, lines ending in “?” are decisions, and indented lines are the answers. The chart redraws as you type and is saved in your browser.

Can I download it?

Yes: PNG for documents and slides, SVG for sharp printing, or Mermaid code for GitHub, Notion and Obsidian.