← 返回资讯屏轩教育 · 行业资讯

How to Use Trace Tables for Hand Execution of Algorithms | 用追踪表记录算法手工运行

2026-08-30 15:25 · 本文部分内容由AI辅助创作

📚 How to Use Trace Tables for Hand Execution of Algorithms | 用追踪表记录算法手工运行

A trace table is a systematic way of recording the values of variables, conditions, and outputs at each step of an algorithm's execution. It is one of the most essential skills for IGCSE Computer Science students, especially when answering questions about algorithms, dry-running code, or debugging logic errors.

追踪表(Trace Table)是一种系统化记录算法执行过程中变量值、条件判断结果和输出内容的方法。对于 IGCSE 计算机科学的学生来说,这是最关键的技能之一,尤其在回答算法题、手工运行代码(Dry Run)或调试逻辑错误时格外重要。


1. What Is a Trace Table? | 什么是追踪表?

A trace table is typically drawn as a table with one column per variable, plus additional columns for conditions and outputs. Each row in the table represents a snapshot of the algorithm's state at a particular moment, usually after one complete statement or instruction has been executed.

追踪表通常是一张表格,每个变量占一列,此外还有条件判断列和输出列。表格中的每一行代表算法在某一特定时刻的状态快照,通常是在一条完整语句或指令执行完毕之后记录一次。

The main purpose of a trace table is to simulate the execution of an algorithm without using a computer. This allows you to:

追踪表的核心目的是在不使用计算机的情况下模拟算法的执行过程,从而帮助你:


2. Setting Up a Trace Table | 建立追踪表的基本步骤

Before you begin hand execution, you need to read the algorithm carefully and identify all the variables that appear. For a loop, you must also decide how many times the loop will repeat. A trace table usually has the following columns:

在开始手工执行之前,你需要仔细阅读算法,找出所有出现过的变量。如果算法中包含循环,你还必须判断循环会重复多少次。一个典型的追踪表通常包含以下列:

In many exam questions, the trace table is already drawn for you, with columns labelled. In others, you must draw the table yourself based on the algorithm given.

在许多考试题目中,追踪表已经预先画好并标注了列名;而在另一些题目中,你需要根据给出的算法自己绘制表格。


3. A Simple Example – Finding the Sum | 简单示例——求和算法

Consider the following algorithm written in pseudocode:

请看以下用伪代码编写的算法:

Now we build a trace table. The variables are Total and Count. There is also an output to record. We will also include a column showing the value of Count at each iteration.

现在我们来建立追踪表。变量是 Total 和 Count,同时还需要记录输出结果。我们还将加入一列,显示每次循环中 Count 的值。

Trace Table | 追踪表

Step Count Total Condition: Count ≤ 3 Output
1 – 0 –
2 1 0 TRUE
3 1 1
4 2 1 TRUE
5 2 3
6 3 3 TRUE
7 3 6
8 4 6 FALSE
9 4 6 6

tops. Notice that the value of Count is 4 in the last rows, even though 4 is never added to Total.

该算法的最终输出是 6,也就是 1 + 2 + 3。追踪 FOR 循环时最关键的步骤是确认循环何时结束。在此例中,当 Count 增加到 4 后,条件 Count ≤ 3 变为 FALSE,循环终止。注意在最后几行中,Count 的值是 4,尽管 4 从未被加到 Total 中。


4. Dry Running with WHILE Loops | 使用 WHILE 循环进行手工运行

WHILE loops are slightly more demanding because the number of iterations is not always obvious at first glance. You must check the condition before every iteration, including the very first one. Consider this example:

WHILE 循环稍微复杂一些,因为循环次数并不总是一眼就能看出。你必须在每次迭代之前检查条件,包括第一次迭代。请看这个例子:

Here is the trace table:

以下是追踪表:

Step N Condition: N > 5 Output
1 10 –
2 10 TRUE
3 7
4 7 TRUE
5 4
6 4 FALSE
7 4 4

Notice that the condition is re-evaluated after N is updated. When N becomes 4, the condition N > 5 is FALSE, so the loop terminates and the output is 4.

请注意,在 N 更新之后,条件被重新评估。当 N 变为 4 时,条件 N > 5 为 FALSE,因此循环结束,输出为 4。


广告 / Sponsored

5. Handling Arrays in Trace Tables | 追踪表中数组的处理方法

When algorithms involve arrays, each element of the array should be given its own column if the array is small (e.g., 5 elements or fewer). If the array is large, exam questions usually provide a table with the array column already prepared, and you only need to write the values of array elements as they appear.

当算法涉及数组时,如果数组规模较小(例如不超过 5 个元素),通常每个数组元素应单独占一列。如果数组很大,考试题目通常会预先准备好带有数组列的表格,你只需在数组元素出现时记录其值即可。

Consider this algorithm that finds the maximum value in a list:

请看这个寻找列表中最大值的算法:

The trace table can be organised as follows. We use one column for the array itself (shown as a fixed list), one for Max, one for i, and one for the IF condition.

追踪表可以这样组织:我们使用一列来表示数组本身(固定列表),一列表示 Max,一列表示 i,还有一列表示 IF 条件的结果。

Step List Max i List[i] > Max Output
1 [4,9,2] – –
2 [4,9,2] 4 –
3 [4,9,2] 4 2 TRUE
4 [4,9,2] 9 2
5 [4,9,2] 9 3 FALSE
6 [4,9,2] 9 3 9

The maximum value of the list is 9. Note that when List[3] equals 2, the condition List[i] > Max is FALSE, so Max is not changed.

该列表的最大值是 9。注意当 List[3] 等于 2 时,条件 List[i] > Max 为 FALSE,因此 Max 不会被更新。


6. Recording Conditions and Boolean Values | 记录条件与布尔值

In IGCSE trace tables, conditions are usually written as TRUE or FALSE. Some students prefer to write T and F to save time, but this can be risky in a formal exam. Writing the full word TRUE or FALSE is clearer and avoids ambiguity. Make sure the condition you record is the exact result of evaluating the expression at that point in time, not the condition as it appears in the code.

在 IGCSE 的追踪表中,条件通常写为 TRUE 或 FALSE。有些学生为了节省时间喜欢写 T 和 F,但在正式考试中这样做有风险。写出完整的 TRUE 或 FALSE 更加清晰,可以避免歧义。你所记录的条件值必须是该时刻表达式求值的真实结果,而不是代码中条件本身的写法。

For example, if a variable X holds the value 7 and the condition is X > 5, you should write TRUE in the condition column. You do not usually need to recalculate the condition if no variable involved in it has changed since the last row.

例如,如果变量 X 的值为 7,条件是 X > 5,你应该在条件列中写 TRUE。如果自上一行以来,条件所涉及的变量没有发生变化,你通常不需要重新计算该条件,只需要在循环重新检查条件时才再记录一次。


7. Common Mistakes to Avoid | 常见错误与注意事项

Students frequently lose marks when hand-tracing algorithms because of avoidable mistakes. Here are the most common ones:

学生在手工追踪算法时经常因为一些可以避免的错误而失分。以下是最常见的几种:


8. Exam Tips for Trace Table Questions | 追踪表题目的考试技巧

In CIE IGCSE Computer Science Paper 2, you will often be asked to "complete the trace table" or "dry run the algorithm". These questions are worth 4 to 8 marks and reward careful, methodical work.

在 CIE IGCSE 计算机科学 Paper 2 中,你经常会被要求“补全追踪表”或“手工运行该算法”。这类题目通常占 4 到 8 分,考查的是细致而有条理的工作能力。


9. Worked Example – Nested Loops | 完整示例——嵌套循环的追踪

mmon source of confusion. Consider this algorithm that outputs a 2 × 2 pattern of numbers:

嵌套循环是常见的易错点。请看这个输出 2 × 2 数字矩阵的算法:

Trace Table | 追踪表

Step i j i * j Output
1 1 –
2 1 1
3 1 1 1 1
4 1 2
5 1 2 2 2
6 2 –
7 2 1
8 2 1 2 2
9 2 2
10 2 2 4 4

The outputs are 1, 2, 2, 4 in that order. Notice how the inner loop (j) runs completely for each value of the outer loop (i).

输出依序为 1、2、2、4。请注意,内层循环(j)会针对外层循环(i)的每一个值完整地运行一遍。


10. Using Trace Tables to Debug Logic Errors | 用追踪表调试逻辑错误

Trace tables are not only for exams. In real-world programming, a trace table can help you detect errors such as an incorrect initial value, a wrong comparison operator, or an off-by-one error in a loop. By writing down each variable's value line by line, you can pinpoint the exact line where the algorithm begins to behave unexpectedly.

追踪表不仅用于考试。在实际编程中,追踪表可以帮助你检测错误,例如初始值设置错误、比较

Published by TutorHao | IGCSE Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao