Two Views of the Same Process

Suppose we want to add the integers from \(1\) through \(4\).

One pseudocode representation is:

total = 0

FOR i = 1 TO 4
    total = total + i
END FOR

A trace table shows what happens as the algorithm runs:

Step \(i\) total
start—0
111
223
336
4410

The pseudocode describes the rule. The table records the evolving state.

\[ \boxed{ \text{pseudocode describes execution;} \quad \text{trace tables record execution} } \]

Why Use Tables?

Tables organize related information into rows and columns.

In computational thinking, a table can represent:

A table is especially useful when the same variables must be observed across many steps.

What Is Pseudocode?

Pseudocode is a structured way to describe an algorithm using plain language and programming-like notation without requiring the exact syntax of a particular programming language.

For example:

INPUT x
y = 2 * x + 3
OUTPUT y

The same idea could later be implemented in Python, JavaScript, C++, or another language.

Pseudocode focuses on the algorithm rather than language-specific punctuation.

Common Pseudocode Conventions

There is no single universal pseudocode language, but several conventions are widely useful.

INPUT

Receive a value or data structure.

OUTPUT / RETURN

Produce or return a result.

IF / ELSE

Choose among branches.

FOR / WHILE

Repeat operations.

Assignment may be written in several ways:

\[ x\leftarrow x+1 \]

or in pseudocode:

x = x + 1

In this context, the statement means “replace the current value of \(x\) with the old value plus \(1\).”

Representing Sequential Steps

Straight-line algorithms execute steps in order.

Example

Compute

\[ y=3x-5. \]
INPUT x
y = 3 * x
y = y - 5
OUTPUT y

Each line changes the current state or produces an output.

The order matters:

\[ 3x-5 \neq 3(x-5). \]

Representing Decisions

Pseudocode can make branching structure explicit.

Example: Sign Classification

IF x > 0
    OUTPUT "positive"
ELSE IF x < 0
    OUTPUT "negative"
ELSE
    OUTPUT "zero"
END IF

Indentation shows which steps belong to each branch.

Clear indentation is not merely cosmetic; it reveals the algorithm's logical structure.

Representing Repetition

Fixed-Count Loop

FOR i = 1 TO 5
    OUTPUT i
END FOR

Condition-Controlled Loop

WHILE x > 10
    x = x / 2
END WHILE

The first states the number or range of repetitions directly.

The second repeats until the condition becomes false.

Named Procedures and Functions

A reusable algorithm can be given a name.

Example

SQUARE(x)
    RETURN x * x
END

Then

SQUARE(5)

returns

\[ 25. \]

Named procedures make it possible to build larger algorithms from smaller ones.

Trace Tables

Pseudocode connected to a trace table The left side shows pseudocode for a running sum and the right side shows rows of a trace table with changing counter and total values. Pseudocode total = 0 FOR i = 1 TO 4 total = total + i END FOR OUTPUT total Trace table i total — 0 1 1 2 3 3 6 4 10 execute
Pseudocode states the rule; the trace table records the states produced while that rule is executed.

A trace table records the values of selected variables as an algorithm executes.

The table may include:

Dry Running an Algorithm

A dry run means executing an algorithm manually with chosen inputs.

Suppose:

x = 3
y = 2 * x
x = x + 4
y = y + x

Trace it carefully:

After step \(x\) \(y\)
start3—
\(y=2x\)36
\(x=x+4\)76
\(y=y+x\)713

The final state is

\[ \boxed{(x,y)=(7,13)}. \]

Dry runs help find logic errors before an algorithm is implemented.

Condition and Decision Tables

Tables are also useful when several conditions interact.

Suppose access requires both a valid password and an active account.

Password valid? Account active? Grant access?
FalseFalseNo
FalseTrueNo
TrueFalseNo
TrueTrueYes

This table is a practical version of the logical rule

\[ \boxed{ P\land A }. \]

Decision tables make it easier to see whether every case has been considered.

Tracing Recursive Procedures

Recursive procedures are often easier to understand when calls and returns are organized in a table.

For factorial:

\[ n!= \begin{cases} 1,&n=0,\\ n(n-1)!,&n>0, \end{cases} \]

a trace for \(3!\) can be recorded as:

Call Waiting operation Returned value
\(\operatorname{fact}(3)\)\(3\cdot\operatorname{fact}(2)\)6
\(\operatorname{fact}(2)\)\(2\cdot\operatorname{fact}(1)\)2
\(\operatorname{fact}(1)\)\(1\cdot\operatorname{fact}(0)\)1
\(\operatorname{fact}(0)\)base case1

The table separates the downward calls from the values that eventually return.

Translating among Representations

The same process can appear in many forms.

Natural Language

“Start at \(1\), double the value three times, then report the result.”

Pseudocode

x = 1

REPEAT 3 TIMES
    x = 2 * x
END REPEAT

OUTPUT x

Trace Table

Step \(x\)
start1
12
24
38

Sequence

\[ 1,\ 2,\ 4,\ 8. \]

Recursive Rule

\[ a_0=1, \qquad a_{n+1}=2a_n. \]

These are different representations of the same repeated structure.

Writing Good Pseudocode

Good pseudocode should be:

Compare:

Poor: “Keep doing stuff until the number looks small.”

Better:

WHILE x >= 10
    x = x / 10
END WHILE

The second version can be traced, tested, and implemented.

Practical Applications

Algorithm Design

Pseudocode lets a designer work out logic before worrying about programming syntax.

Debugging

Trace tables reveal exactly where variable values stop matching expectations.

Mathematics

Tables help trace sequences, iterative approximations, recurrence relations, and row-by-row procedures.

Education

Pseudocode exposes the structure of an algorithm without requiring students to learn a full programming language first.

Communication

A table or pseudocode block can make a process easier for another person to review than a long paragraph of prose.

Worked Examples

Example 1: Formula to Pseudocode

Translate

\[ y=4x+1 \]

into pseudocode.

INPUT x
y = 4 * x + 1
OUTPUT y

Example 2: Trace a Sequence of Assignments

x = 2
y = x + 3
x = y * 2
After step \(x\) \(y\)
\(x=2\)2—
\(y=x+3\)25
\(x=2y\)105

Final state:

\[ \boxed{(x,y)=(10,5)}. \]

Example 3: Trace a Loop

total = 1

FOR i = 1 TO 3
    total = total * 2
END FOR
Iteration \(i\) total
start—1
112
224
338

Output:

\[ \boxed8. \]

Example 4: Trace a Branch

x = -4

IF x >= 0
    y = x
ELSE
    y = -x
END IF

Since

\[ -4\geq0 \]

is false, the else branch runs:

\[ y=-(-4)=4. \] \[ \boxed{y=4} \]

Example 5: Decision Table

A user receives free shipping if the order is at least \(\$50\) or the user is a premium member.

Order ≥ \$50? Premium? Free shipping?
NoNoNo
NoYesYes
YesNoYes
YesYesYes

The logical rule is

\[ \boxed{O\lor P}. \]

Example 6: Pseudocode to Mathematical Rule

IF x > 0
    RETURN x^2
ELSE
    RETURN 0
END IF

The equivalent piecewise function is

\[ \boxed{ f(x)= \begin{cases} x^2,&x>0,\\ 0,&x\leq0. \end{cases} } \]

Common Errors

1. Writing Programming Syntax Instead of Algorithmic Structure

Pseudocode should emphasize the process, not unnecessary language-specific details.

2. Failing to Update a Trace Table after Every Relevant Step

A skipped state can hide the exact point where an error occurs.

3. Treating Assignment as Ordinary Equality

In

x = x + 1

the left side receives a new value. This is not the algebraic equation \(x=x+1\).

4. Losing Track of Variable Values after Reassignment

A trace table should always use the most recent state.

5. Using Poor Indentation

Branches and loops become ambiguous when their bodies are not visually grouped.

6. Recording Only the Final Answer

A trace table is useful precisely because it records intermediate states, not just the final output.

Practice

Trace Assignments

x = 4
y = x + 2
x = y - 1

What is the final state?

Check Answer \[ y=6, \] \[ x=5. \] \[ \boxed{(x,y)=(5,6)}. \]

Trace a Loop

total = 0

FOR i = 1 TO 3
    total = total + 2
END FOR
Check Answer

The totals are

\[ 0,\ 2,\ 4,\ 6. \]

Final output:

\[ \boxed6. \]

Translate to Pseudocode

Write pseudocode for: “Input \(x\). If \(x<0\), output \(-x\); otherwise output \(x\).”

Check Answer
INPUT x

IF x < 0
    OUTPUT -x
ELSE
    OUTPUT x
END IF

Choose the Better Representation

Which representation is usually better for seeing how a variable changes across ten loop iterations: pseudocode alone or a trace table?

Check Answer \[ \boxed{\text{trace table}} \]

because it records the state at each iteration.

Decision Table

If a rule requires both condition \(P\) and condition \(Q\), for which truth combination should the output be true?

Check Answer

Only when both are true:

\[ \boxed{P=\text{true},\ Q=\text{true}}. \]

The Deeper Idea

Pseudocode and tables are complementary representations.

\[ \boxed{ \text{pseudocode} \to \text{rules of transformation} } \] \[ \boxed{ \text{trace table} \to \text{states produced by transformation} } \]

This connects directly to earlier ideas:

A trace table is therefore not merely bookkeeping.

It is a representation of execution.

And pseudocode is not merely simplified code.

It is a representation of algorithmic structure.

Summary

Pseudocode tells us what the algorithm says to do. Tables let us watch what the algorithm actually does to its data. Used together, they provide a powerful bridge between mathematical reasoning, algorithm design, and later computer programming.

Algorithms and Computational Thinking