Skip to content

Flowcharts

Learn to visualize program logic with flowcharts — decision diamonds, process boxes, and flow arrows.

What you'll learn

  • What flowcharts are and the standard symbols
  • How to map if/else and loops to flowchart shapes
  • Reading and tracing a flowchart step by step
  • Using flowcharts to find logic errors visually
  • When flowcharts help and when pseudocode is better

Concept

Flowcharts

A flowchart is a visual diagram of a program's logic. Where pseudocode uses words, flowcharts use shapes and arrows. Each shape represents a type of operation, and arrows show the flow of execution. For visual thinkers, flowcharts make program logic easier to understand than text — you can see the paths, the decisions, and the loops at a glance.

Standard Symbols

Flowcharts use a small set of standard shapes:

| Shape | Meaning | Example | |-------|---------|---------| | Oval (terminator) | Start or end | "Start", "End" | | Rectangle (process) | An action or calculation | "sum = sum + value" | | Diamond (decision) | A yes/no question | "is sum > 100?" | | Parallelogram (I/O) | Input or output | "read user input" | | Arrow (flow) | Direction of execution | connects the shapes |

Mapping Code to Flowchart

Every programming construct has a flowchart equivalent:

  • Sequence (line after line) — rectangles connected by arrows, top to bottom.
  • if/else (branching) — a diamond with two arrows: one labeled "yes" for the true branch, one labeled "no" for the false branch. Both arrows rejoin after the branches complete.
  • while loop (repetition) — a diamond at the top of a loop. The "yes" arrow goes through the loop body and back to the diamond. The "no" arrow exits the loop.
  • for loop — same as while, but the diamond includes the counter check (e.g., "i < 10?").

Tracing a Flowchart

To understand what a flowchart does, trace it: start at the oval, follow the arrows, and write down the value of each variable at each step. When you hit a diamond, evaluate the condition and follow the matching arrow. This is the visual equivalent of running the program in your head — and it is a powerful debugging technique.

Finding Errors Visually

Flowcharts expose logic errors that are hard to see in code. If a diamond has no "no" arrow, the loop never exits — an infinite loop, obvious in a diagram. If two branches never rejoin, there is a missing merge point. If a process box is unreachable (no arrows lead to it), there is dead code. These structural problems jump out in a diagram but hide in text.

Flowcharts vs. Pseudocode

Flowcharts and pseudocode are complementary tools, not competitors:

  • Flowcharts are best for visualizing branching and looping — the *shape* of the logic. They excel at showing all possible paths through a program.
  • Pseudocode is best for specifying computations — the *content* of each step. It excels at showing what each line does.

For simple sequences, pseudocode is faster. For complex branching with many conditions, a flowchart is clearer. Many developers sketch a quick flowchart for the tricky part, then write pseudocode for the details. Use both — they serve different purposes.

When Not to Use Flowcharts

For large programs, flowcharts become unwieldy — a 500-line program would need a wall-sized diagram. Use flowcharts for individual functions or algorithms, not entire applications. For system-level design, use other tools: architecture diagrams, sequence diagrams, or component diagrams. Flowcharts are a tool for the *logic* level, not the *system* level.

Example

              // Problem: Check if a number is prime.
// Flowchart logic:
//   [Start] → [Input n] → [n < 2?] --yes--> [Output: false] → [End]
//                          |no
//                          v
//                    [i = 2]
//                          |
//                    <i * i <= n?> --no--> [Output: true] → [End]
//                          |yes
//                          v
//                    <n % i == 0?> --yes--> [Output: false] → [End]
//                          |no
//                          v
//                    [i = i + 1] --> (loop back to i*i <= n)

// Code translated from the flowchart:
function isPrime(n) {
  if (n < 2) return false;          // Decision: n < 2?
  for (let i = 2; i * i <= n; i++) { // Decision: i*i <= n? (loop)
    if (n % i === 0) return false;   // Decision: n % i == 0?
  }
  return true;                       // All checks passed
}

// Trace the flowchart with n = 7:
// n < 2? No → i=2 → i*i=4 <= 7? Yes → 7%2==0? No → i=3
// i*i=9 <= 7? No → Output: true (7 is prime)
console.log(isPrime(7));   // true
console.log(isPrime(8));   // false (8 % 2 == 0)
console.log(isPrime(2));   // true
            

The comment block shows the flowchart in ASCII art. Each decision (diamond) maps to an if statement. The loop maps to the for loop. Tracing the flowchart with n=7 confirms the logic before running the code.

Try it

  • Regex Explainer

    Regular expressions have branching logic (alternation, quantifiers). The explainer visualizes the decision flow — similar to reading a flowchart.

  • Crontab Generator

    Cron schedules have conditional logic (if day matches, if hour matches). Visualizing the schedule flow helps understand the timing logic.

Common mistakes

The mistake

Drawing flowcharts that are too detailed, showing every line of code.

The fix

Flowcharts should show the logic structure — decisions, loops, and major steps — not every assignment statement. Keep them at the level of 'what decision is being made' and 'what block of work is done'.

The mistake

Forgetting to label the yes/no arrows coming out of decision diamonds.

The fix

Every diamond must have two labeled arrows (yes/true and no/false). Unlabeled arrows make the flowchart ambiguous — the reader cannot tell which path each condition takes.

The mistake

Creating flowcharts with arrows that cross or loop back to unclear points.

The fix

Lay out the flowchart top-to-bottom with minimal crossing lines. Loop-back arrows should clearly return to the loop's decision diamond. If the diagram is tangled, the logic probably is too — simplify both.

Related Cheatsheets

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors