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

📚 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:

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

  • Verify that the algorithm produces the correct output.
  • 检测算法是否能产生正确的输出结果。
  • Identify logic errors such as off-by-one mistakes or incorrect condition boundaries.
  • 发现逻辑错误,例如差一错误(off-by-one)或条件边界设置不当。
  • Understand how variables change over time.
  • 理解变量在算法执行过程中的变化规律。

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:

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

  • Line or Step number – to show which instruction is being executed.
  • 行号或步骤号——用于标明正在执行的是哪一条指令。
  • Each variable name – e.g., Total, Count, X, Y.
  • 每个变量名——例如 Total、Count、X、Y。
  • Condition column – to record whether a condition evaluates to TRUE or FALSE.
  • 条件列——用于记录条件判断的结果是 TRUE(真)还是 FALSE(假)。
  • Output column – for any values printed or displayed.
  • 输出列——用于记录任何打印或显示的值。

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:

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

  • Total ← 0
  • Total ← 0
  • FOR Count ← 1 TO 3
  • FOR Count ← 1 TO 3
  • Total ← Total + Count
  • Total ← Total + Count
  • ENDFOR
  • ENDFOR
  • OUTPUT Total
  • OUTPUT Total

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

The final output of the algorithm is 6, which is 1 + 2 + 3. The trickiest part of tracing a FOR loop is knowing when the loop ends. In this example, after Count is incremented to 4, the condition Count ≤ 3 becomes FALSE, so the loop stops. 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 循环稍微复杂一些,因为循环次数并不总是一眼就能看出。你必须在每次迭代之前检查条件,包括第一次迭代。请看这个例子:

  • N ← 10
  • N ← 10
  • WHILE N > 5
  • WHILE N > 5
  • N ← N − 3
  • N ← N − 3
  • ENDWHILE
  • ENDWHILE
  • OUTPUT N
  • OUTPUT N

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。


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:

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

  • List ← [4, 9, 2]
  • List ← [4, 9, 2]
  • Max ← List[1]
  • Max ← List[1]
  • FOR i ← 2 TO 3
  • FOR i ← 2 TO 3
  • IF List[i] > Max THEN
  • IF List[i] > Max THEN
  • Max ← List[i]
  • Max ← List[i]
  • ENDIF
  • ENDIF
  • ENDFOR
  • ENDFOR
  • OUTPUT Max
  • OUTPUT Max

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:

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

  • Not updating all variables after each line. For example, forgetting to increment the loop counter.
  • 没有在每一行之后更新所有变量,例如忘记递增循环计数器。
  • Copying the previous value into a cell when the variable has not actually changed, but still failing to notice that a condition should be re-evaluated.
  • 当变量实际上没有变化时,机械地复制上一行的值,却忽略了某些条件需要重新求值。
  • Tracing the loop one iteration too far or stopping one iteration too early.
  • 循环追踪时多推了一轮或少推了一轮。
  • Confusing the order of operations, especially when a variable appears on both sides of an assignment (e.g., Total ← Total + N).
  • 混淆运算顺序,尤其是当赋值语句两侧出现同一个变量时(例如 Total ← Total + N)。
  • Not recording the output at the correct time.
  • 没有在正确的时间点记录输出。

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 分,考查的是细致而有条理的工作能力。

  • Always start with the initial values of variables, even if they are set by the first line of the algorithm.
  • 始终从变量的初始值开始记录,即使这些值是由算法的第一行赋值的。
  • Use a ruler to draw straight lines separating columns and rows in your table if you have to draw it yourself.
  • 如果需要自己绘制表格,请用尺子画直线来分隔列和行。
  • Write legibly. If the examiner cannot read a value, you will not get the mark.
  • 书写要清晰工整。如果考官无法辨认某个值,你就得不到对应的分数。
  • Check the final output against your own intuition. If the question gives you sample data, verify your result manually.
  • 将最终输出与直觉判断进行核对。如果题目给出了示例数据,请手动验证你的结果。
  • When a condition is checked in a loop, record TRUE or FALSE every time the loop might be entered, not just when it is true.
  • 在循环中检查条件时,每次判断循环是否应继续时都要记录 TRUE 或 FALSE,而不仅仅是在条件为 TRUE 的时候才记录。

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

Nested loops are a common source of confusion. Consider this algorithm that outputs a 2 × 2 pattern of numbers:

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

  • FOR i ← 1 TO 2
  • FOR i ← 1 TO 2
  • FOR j ← 1 TO 2
  • FOR j ← 1 TO 2
  • OUTPUT i * j
  • OUTPUT i * j
  • ENDFOR
  • ENDFOR
  • ENDFOR
  • ENDFOR

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

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

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version