如何 Programs 工作
学习如何 program 运行步骤通过步骤, 什么 execution 顺序平均值, and 如何 programs 制作 decisions.
你将学到
- 如何 program 执行行 通过行
- 什么 execution flow 是 and 为什么它 matters
- 如何 branching 更改顺序的 execution
- role 的 memory 虽然 program 运行
概念讲解
How Programs Work
When a program runs, the computer reads your instructions in order, starting from the top and moving down. This is called sequential execution. Each line is performed completely before the next one begins.
Execution Flow
The path the computer takes through your code is called the flow of execution. By default it is a straight line from top to bottom. But real programs rarely just run straight down — they branch, loop, and call functions, which change the flow.
- Branching (
如果/else) lets the program choose a different path based on a condition. - Looping (
for/虽然) makes the program repeat a section multiple times. - Functions let the program jump to a named block of code, run it, and return.
Memory While Running
While a program runs, it uses memory to store the data it is working with. When you write const age = 25, the program reserves a spot in memory, puts the value 25 there, and labels it age so you can refer to it later. When the program finishes, that memory is released.
Why Order Matters
Because execution is ordered, the same lines in a different order produce a different result. This is the #1 surprise for beginners. Moving one line up or down can change what your program does — or break it entirely. Learning to read code "in computer's 顺序" is a skill you build with practice.
示例代码
// Sequential execution — order matters!
const price = 10;
const quantity = 3;
const total = price * quantity; // 30
console.log("Total:", total);
// Branching changes the flow:
if (total > 25) {
console.log("You qualify for free shipping.");
} else {
console.log("Shipping costs $5.");
}
program 运行顶部到 底部. 如果/else 是分支: 仅 one 的 two 消息将 print, depending on whether 总计 exceeds 25.
动手试一试
- 文本计数器
Watch program 处理文本步骤通过步骤: 它数量 characters, words, and 行 — 每个 sequential 操作.
常见错误
错误写法
Assuming code 运行所有 at 一次相反的 行通过行.
正确做法
读取你的 code 顶部到 底部, imagining one 行执行 at 时间. 这个 mental model prevents 大多数排序 bugs.
错误写法
Using 变量之前它 有是 given 值.
正确做法
Declare and assign 变量之前你 use 它. program 读取顶部到 底部, 所以变量必须 exist 通过时间行 引用它.
相关资源
相关代码片段
- JavaScript 数组方法
See 如何 flow 工作使用 chained 操作 on 数组.
练习 如何 Programs 工作
4 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。