Skip to content

Flowcharts

学习到 visualize program logic 使用 flowcharts — decision diamonds, 处理 boxes, and flow arrows.

你将学到

  • 什么 flowcharts 是 and 标准 symbols
  • 如何 映射如果/else and 循环到 flowchart shapes
  • 读取 and tracing flowchart 步骤通过步骤
  • Using flowcharts 到查找 logic 错误 visually
  • 当 flowcharts help and 当 pseudocode 是 better

概念讲解

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 | "开始", "端" | | Rectangle (process) | An action or calculation | "总和 = 总和 + 值" | | Diamond (decision) | A yes/no question | "是总和 > 100?" | | Parallelogram (I/O) | Input or output | "读取用户输入" | | 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 "是" for the true branch, one labeled "否" for the false branch. Both arrows rejoin after the branches complete.
  • while loop (repetition) — a diamond at the top of a loop. The "是" arrow goes through the loop body and back to the diamond. The "否" 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 "否" 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.

示例代码

              // 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
            

comment 块 shows flowchart in ASCII art. 每个 decision (diamond) 映射到 如果语句. 循环映射到 for 循环. Tracing flowchart 使用 n=7 确认 logic 之前运行 code.

动手试一试

  • Regex Explainer

    Regular 表达式有 branching logic (alternation, quantifiers). explainer visualizes decision flow — 相似到 读取 flowchart.

  • Crontab Generator

    Cron schedules 有 conditional logic (如果 day matches, 如果 hour matches). Visualizing schedule flow helps 理解 timing logic.

常见错误

错误写法

Drawing flowcharts 那个是 也 detailed, showing 每个行 的 code.

正确做法

Flowcharts 应该 show logic 结构 — decisions, 循环, and major steps — 不每个 assignment 语句. Keep 它们 at level 的 '什么 decision 是是 制作' and '什么块 的工作是 做'.

错误写法

Forgetting 到 label 是/否 arrows coming out 的 decision diamonds.

正确做法

每个 diamond 必须有 two labeled arrows (是/真 and 否/假). Unlabeled arrows 制作 flowchart ambiguous — reader cannot tell 哪个 path 每个条件 takes.

错误写法

创建 flowcharts 使用 arrows 那个 cross or 循环后面到 unclear 点.

正确做法

Lay out flowchart 顶部-到-底部使用 minimal crossing 行. 循环-后面 arrows 应该清晰地返回到 循环's decision diamond. 如果 diagram 是 tangled, logic probably 是也 — simplify 两者都.

相关速查表

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误