01 / 21

Explaining
Code Behavior

Learn to read, trace, and explain what C++ code does, step by step, using precise programming terminology.

C++

Why Explain Code Behavior?

Writing code is only half the battle. Being able to read someone else's code (or your own, weeks later) and explain exactly what happens at each step is the mark of a strong programmer. It helps you debug faster, communicate with teammates, and ace exams.

Debugging
Tracing code line by line reveals where logic breaks down.
Communication
Explaining code to others proves you truly understand it.
Exams
Many CS assessments ask "What does this code output?"

Key Terminology

Declaration
Introducing a variable's name and type: int x;
Initialization
Giving a variable its first value: int x = 5;
Assignment
Changing a variable's value after declaration: x = 10;
Expression
A combination of values and operators that produces a result: x + 3
Statement
A complete instruction ending with a semicolon: cout << x;

The Execution Model

C++ executes code sequentially: top to bottom, one statement at a time, unless a control structure (if, for, while) redirects the flow. To explain code, you must follow this exact order.

main() begins Declaration / Init Computation Output (cout) return 0; (program ends) Line 1 Line 2 Line 3 Line 4

The Golden Rule of Code Tracing

int x = 5; int y = x + 3; // right side: 5 + 3 = 8, then store 8 in y x = x + 1; // right side: 5 + 1 = 6, then store 6 in x

Variables as Named Boxes

Think of each variable as a labeled box in memory. Declaration creates the box. Initialization puts the first value inside. Assignment replaces whatever is currently inside.

x 5 int x = 10; x 10 int y 7 int BEFORE AFTER UNCHANGED

Declaration vs Assignment vs Initialization

Declaration Assignment Initialization
What it does Creates a variable Changes its value Creates + sets value
Example int x; x = 10; int x = 10;
Type keyword? Yes No Yes
Variable exists? Not yet Already Not yet

Step-by-Step Trace Method

1
Draw a variable table. Create a column for each variable and a row for each line of code.
2
Start at the first line of main(). Read the statement and identify what it does: declaration, assignment, output, or control flow.
3
Evaluate the right side first. Substitute current variable values, compute the expression, then store the result.
4
Update the table. Write the new value in the appropriate cell and cross out the old value.
5
Move to the next line. Repeat until you reach return 0; or the closing brace of main().

Example 1: Simple Arithmetic

#include <iostream> using namespace std; int main() { int a = 4; int b = 7; int c = a + b; cout << c << endl; return 0; }
1
Declare and initialize a to 4
2
Declare and initialize b to 7
3
Evaluate \(a + b \rightarrow 4 + 7 = 11\), store in c
4
Output the value of c to the console
Console Output
11

Example 2: Variable Reassignment

int x = 3; int y = x * 2; x = x + y; y = x - 1; cout << x << " " << y << endl;
1
x initialized to 3
2
\(x \times 2 \rightarrow 3 \times 2 = 6\), y set to 6
3
\(x + y \rightarrow 3 + 6 = 9\), x updated to 9
4
\(x - 1 \rightarrow 9 - 1 = 8\), y updated to 8
5
Output: print x then a space then y
Console Output
9 8

Example 3: Conditionals (if/else)

int score = 85; if (score >= 90) { cout << "A" << endl; } else if (score >= 80) { cout << "B" << endl; } else { cout << "C" << endl; }
1
score initialized to 85
2
Check: \(85 \ge 90\)? false → skip the if block
3
Check: \(85 \ge 80\)? true → enter the else if block
4
Output "B". The remaining else is skipped entirely.
Console Output
B

Tracing a for Loop

for (int i = 0; i < 4; i++) { cout << i << " "; } cout << endl;
1
Init: \(i = 0\). Check \(0 < 4\)? true → print 0
2
Update: \(i\text{++} \rightarrow i = 1\). Check \(1 < 4\)? true → print 1
3
Update: \(i\text{++} \rightarrow i = 2\). Check \(2 < 4\)? true → print 2
4
Update: \(i\text{++} \rightarrow i = 3\). Check \(3 < 4\)? true → print 3
5
Update: \(i\text{++} \rightarrow i = 4\). Check \(4 < 4\)? false → exit loop
Console Output
0 1 2 3

Tracing Function Calls

int doubleIt(int n) { return n * 2; } int main() { int a = 5; int b = doubleIt(a); cout << b << endl; return 0; }
1
a initialized to 5
2
Call doubleIt(5): parameter n receives value 5
3
Inside function: evaluate \(n \times 2 \rightarrow 5 \times 2 = 10\), return 10
4
Back in main: returned value 10 is stored in b
5
Output: print value of b
Console Output
10

Pass by Value vs Pass by Reference

Pass by Value
The function gets a copy. Changes inside the function do not affect the original variable.
Pass by Reference (&)
The function gets the original variable. Changes inside the function do affect it.
void addOne(int n) { n = n + 1; } // copy: original unchanged void addOneRef(int& n) { n = n + 1; } // reference: original changed int main() { int x = 10; addOne(x); // x is still 10 addOneRef(x); // x is now 11 cout << x << endl; }
Console Output
11

Common Mistakes

✕ Using old values
Forgetting that a variable was reassigned earlier and using its original value in a later computation.
✕ Ignoring order
Assuming two lines can "see" each other rather than executing strictly top to bottom.
✕ Off-by-one in loops
Confusing < with <=, leading to one extra or one fewer iteration than expected.
✕ Integer division
Writing \(7 / 2\) and expecting \(3.5\) when both operands are int. Result is \(3\) (truncated).
✕ Pass-by-value confusion
Expecting a function to modify the original variable when the parameter is passed by value (no &).

Summary of C++ Code Behavior

Concept What to Say Example
Declaration "A variable of type T is created" int x;
Initialization "Variable is created and set to V" int x = 5;
Assignment "The value V is stored into variable" x = 10;
Expression "The right side evaluates to R" x + y
Conditional "The condition is true/false, so we enter/skip the block" if (x > 0)
Loop "The loop iterates N times; variable goes from A to B" for (\(i=0; i<N; i\text{++}\))
Function call "Control passes to function F with argument V" doubleIt(5)

Question 1 of 5

What is the output of this code?

Question 2 of 5

What is the value of x after running: int x = 7; x = x + x;?

Question 3 of 5

What is the output of this loop?

Question 4 of 5

What does this program output?

Question 5 of 5

Which C++ term best describes the statement int count = 0;?