All Labs

Selection & Iteration Code Tracing Lab

Step through Java code line by line, track variable values in a trace table, and predict console output. Practice the exact skills tested on AP Computer Science A free-response questions with 25 code snippets across 5 categories.

Guided Experiment: Loop Tracing Challenge

Which type of loop (for, while, or nested) do you think will be hardest to trace correctly? Why?

Write your hypothesis in the Lab Report panel, then click Next.

Simple if/elseeasy

1int x = 15;
2String result;
3if (x > 10) {
4 result = "big";
5} else {
6 result = "small";
7}
8System.out.println(result);
Click "Step" or "Play" to begin tracing

Controls

Execution Step / 5
Auto-play
Predict Mode

Hide output and predict the result

Console Output

No output yet

Execution Metrics

Iterations
0
Assignments
3
Comparisons
1
Output Lines
1
Trace Progress0%

Variable Trace

Step through the code to see variable values

Data Table

(0 rows)
#SnippetCategoryVariablesIterationsOutput LinesPredicted Correctly?
0 / 500
0 / 500
0 / 500

Reference Guide

if/else Selection

Selection statements choose which code block to execute based on a boolean condition. Java evaluates the condition and follows exactly one branch.

Key patterns include simple if/else, chained else-if ladders, nested if statements, and the ternary operator. When tracing, evaluate each condition top to bottom and follow the first true branch.

Common mistake: forgetting that only the first matching branch executes in an else-if chain. Once a condition is true, all remaining else-if and else blocks are skipped.

for & while Loops

A for loop has three parts: initialization, condition check, and update. The condition is checked before each iteration, and the update runs after the body.

A while loop checks its condition before each iteration. The loop variable must be updated inside the body to avoid infinite loops.

Tracing strategy: make a table with one column per variable. Fill in a new row for each line that changes a value. Pay attention to the loop variable's final value after the loop exits.

Nested Loops

When one loop is inside another, the inner loop runs completely for each iteration of the outer loop. If the outer loop runs M times and the inner runs N times, the body executes M x N times total.

Pattern printing: the outer loop typically controls the row, and the inner loop controls the column. The inner loop's bound often depends on the outer loop variable to create triangular or pyramid shapes.

2D array traversal: use the outer loop for rows and the inner loop for columns. Access elements with grid[r][c].

Code Tracing Strategy

Step 1: identify all variables and their initial values. Create a trace table with one column per variable.

Step 2: execute one line at a time. For each line, update the variable that changes and note any output.

Step 3: for loops, track the loop variable carefully through the condition check, body, and update. Write down the variable value at the start of each iteration.

Step 4: collect all System.out.println and System.out.print output in order. Remember that println adds a newline but print does not.