Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

4.5 Informal Code Analysis

6 min readjanuary 7, 2023

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

🗺️ Tracing

The majority of the AP Computer Science A multiple-choice questions ask you to trace the code, so it is imperative that you master this concept. Tracing is when you read through the code line by line and keep track of the variables and their values, so you know exactly what is going on in your program. Tracing code is useful for debugging as well. Fortunately, the AP exam will only ask you to trace short snippets of code.

🔎 How Do We Trace Code?

You use a table to keep track of the values and variables. Let's walk through an example with a for loop:

for (int i = 1; i <= 5; i++) {
	System.out.println(i + 10);
}

Let’s break it down it! The first line of this for loop creates a loop counter variable i and assigns it the value 1. It limits the loop to run only while i is less than or equal to 5 and increments i by 1 each time the loop is run. Let's create a table with this information.

Iteration #i
11

We listed the iteration number (the number of times the loop has run) and the value of the loop counter variable i. This is the first iteration, and i was initialized with the value 1.

The body of the loop contains a print statement that prints ten more than the value of i. Let's add this to our table.

Iteration #ii + 10
1111

The first iteration of this loop will print 11 to the console. Now we can go back to the top of the loop and repeat this process until the loop condition becomes false. Try this on your own with a pencil and paper, then look below to check your work. Here is the finished table:

Iteration #ii + 10
1111
2212
3313
4414
5515

From our tracing table, we can tell that the output of this for loop is

11
12
13
14
15

Often, the multiple-choice questions will ask you how many times the loop runs or what the output of the loop is, so meticulous tracing of the code is important so you don't choose 6 iterations when the answer is 7 iterations.

This was a fairly simple example. Try this next one on your own, then read through the explanation below:

What is the output of this code?

for (int i = 0; i < 6; i += 2) {
	for (int j = 3; j <= 6; j += 3) {
		System.out.println(i+j);
	}
System.out.println("*");
}

Explanation: The first line declares an integer variable i and initializes it to 0. It also starts a for loop that will continue to execute as long as i is less than 6. The for loop will iterate 3 times because i is increased by 2 at the end of each iteration. The second line declares an integer variable j and initializes it to 3. It also starts a nested for loop that will continue to execute as long as j is less than or equal to 6. The inner for loop will iterate 2 times for each iteration of the outer loop, because j is increased by 3 at the end of each iteration. The number of times the inner loop iterates is equal to the number of times the outer loop iterates multiplied by the number of times the inner loop iterates, so 3*2=6. This is our tracing table so far:

Outer Loop Iteration #Inner Loop Iteration #ij
1103

The third line prints the sum of i and j to the console on a new line.

Outer Loop Iteration #Inner Loop Iteration #iji + j
11033

The fifth line is run only in the scope of the outside loop. In this line, an asterisk is printed to the console. Putting all these pieces together, our tracing table looks like this:

Outer Loop Iteration #Inner Loop Iteration #iji + jAsterisk?
11033no
12066no
10*
21235no
22268no
22*
31437no
324610no
34*

The two columns on the right track the output of the nested for loop, and combining them yields:

3

6

*

5

8

*

7

10

That may have been tedious, but it was worth it to make sure there were no errors in our tracing. Be sure to practice with the practice problems below to perfect your tracing skills and ace the multiple-choice portion of the AP exam.


🏋️‍♂️ Practice Problems

  1. What is the output of this loop?
for (int i = 0; i < 5; i++) {
	System.out.print(i + " ");
}

a) 0 1 2 3 4

b) 1 2 3 4 5

c) 0 1 2 3

d) 1 2 3 4

  1. What is the output of this loop?
for (int i = 1; i <= 5; i++) {
	for (int j = 1; j <= i; j++) {
		System.out.print("*");
	}
System.out.println();
}

a)

*

**

***

b)

****

***

**

*

c)

*****

****

***

**

d)

*

**

***

****

*****

  1. How many times does the inner loop run?
for (int i = 1; i <= 5; i++) {
	for (int j = 1; j <= 3; j++) {
		System.out.print(j + " ");
	}
System.out.println();
}

a) 5

b) 8

c) 15

d) 20

  1. What is the output of this code?
int i = 1;
while (i <= 5) {
	int j = 1;
	while (j <= i) {
		System.out.print(i + " ");
		j++;
	}
	System.out.println();
	i++;
}

a) 12345

b)

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

c)

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

d)

1

22

333

4444

55555

  1. What is the output of this code?
int i = 1;
while (i <= 5) {
	int j = 5;
	while (j >= i) {
		System.out.print("*");
		j--;
	}
System.out.println();
i++;
}

a)

*****

****

***

b)

*****

****

***

**

*

c)

*

**

***

d)

*

**

***

****

*****


👀 Answers to Practice Problems

  1. a
  2. d
  3. c
  4. c
  5. b

Key Terms to Review (7)

For Loop

: A for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times or until certain conditions are met.

Iteration

: Iteration refers to the process of repeating a set of instructions multiple times in order to achieve a desired outcome. It allows for efficient execution of repetitive tasks without having to write redundant code.

Nested For Loop

: A nested for loop is a loop structure where one loop is placed inside another loop. It allows for the repetition of a set of instructions within another set of instructions, resulting in more complex and controlled iterations.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

Tracing

: Tracing refers to the process of following the execution of a program step by step to understand how it works and identify any errors or bugs.

Variables

: Variables are named storage locations in computer memory that hold values which can be changed during program execution.

while loop

: A while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true.

4.5 Informal Code Analysis

6 min readjanuary 7, 2023

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

Kashvi Panjolia

🗺️ Tracing

The majority of the AP Computer Science A multiple-choice questions ask you to trace the code, so it is imperative that you master this concept. Tracing is when you read through the code line by line and keep track of the variables and their values, so you know exactly what is going on in your program. Tracing code is useful for debugging as well. Fortunately, the AP exam will only ask you to trace short snippets of code.

🔎 How Do We Trace Code?

You use a table to keep track of the values and variables. Let's walk through an example with a for loop:

for (int i = 1; i <= 5; i++) {
	System.out.println(i + 10);
}

Let’s break it down it! The first line of this for loop creates a loop counter variable i and assigns it the value 1. It limits the loop to run only while i is less than or equal to 5 and increments i by 1 each time the loop is run. Let's create a table with this information.

Iteration #i
11

We listed the iteration number (the number of times the loop has run) and the value of the loop counter variable i. This is the first iteration, and i was initialized with the value 1.

The body of the loop contains a print statement that prints ten more than the value of i. Let's add this to our table.

Iteration #ii + 10
1111

The first iteration of this loop will print 11 to the console. Now we can go back to the top of the loop and repeat this process until the loop condition becomes false. Try this on your own with a pencil and paper, then look below to check your work. Here is the finished table:

Iteration #ii + 10
1111
2212
3313
4414
5515

From our tracing table, we can tell that the output of this for loop is

11
12
13
14
15

Often, the multiple-choice questions will ask you how many times the loop runs or what the output of the loop is, so meticulous tracing of the code is important so you don't choose 6 iterations when the answer is 7 iterations.

This was a fairly simple example. Try this next one on your own, then read through the explanation below:

What is the output of this code?

for (int i = 0; i < 6; i += 2) {
	for (int j = 3; j <= 6; j += 3) {
		System.out.println(i+j);
	}
System.out.println("*");
}

Explanation: The first line declares an integer variable i and initializes it to 0. It also starts a for loop that will continue to execute as long as i is less than 6. The for loop will iterate 3 times because i is increased by 2 at the end of each iteration. The second line declares an integer variable j and initializes it to 3. It also starts a nested for loop that will continue to execute as long as j is less than or equal to 6. The inner for loop will iterate 2 times for each iteration of the outer loop, because j is increased by 3 at the end of each iteration. The number of times the inner loop iterates is equal to the number of times the outer loop iterates multiplied by the number of times the inner loop iterates, so 3*2=6. This is our tracing table so far:

Outer Loop Iteration #Inner Loop Iteration #ij
1103

The third line prints the sum of i and j to the console on a new line.

Outer Loop Iteration #Inner Loop Iteration #iji + j
11033

The fifth line is run only in the scope of the outside loop. In this line, an asterisk is printed to the console. Putting all these pieces together, our tracing table looks like this:

Outer Loop Iteration #Inner Loop Iteration #iji + jAsterisk?
11033no
12066no
10*
21235no
22268no
22*
31437no
324610no
34*

The two columns on the right track the output of the nested for loop, and combining them yields:

3

6

*

5

8

*

7

10

That may have been tedious, but it was worth it to make sure there were no errors in our tracing. Be sure to practice with the practice problems below to perfect your tracing skills and ace the multiple-choice portion of the AP exam.


🏋️‍♂️ Practice Problems

  1. What is the output of this loop?
for (int i = 0; i < 5; i++) {
	System.out.print(i + " ");
}

a) 0 1 2 3 4

b) 1 2 3 4 5

c) 0 1 2 3

d) 1 2 3 4

  1. What is the output of this loop?
for (int i = 1; i <= 5; i++) {
	for (int j = 1; j <= i; j++) {
		System.out.print("*");
	}
System.out.println();
}

a)

*

**

***

b)

****

***

**

*

c)

*****

****

***

**

d)

*

**

***

****

*****

  1. How many times does the inner loop run?
for (int i = 1; i <= 5; i++) {
	for (int j = 1; j <= 3; j++) {
		System.out.print(j + " ");
	}
System.out.println();
}

a) 5

b) 8

c) 15

d) 20

  1. What is the output of this code?
int i = 1;
while (i <= 5) {
	int j = 1;
	while (j <= i) {
		System.out.print(i + " ");
		j++;
	}
	System.out.println();
	i++;
}

a) 12345

b)

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

c)

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

d)

1

22

333

4444

55555

  1. What is the output of this code?
int i = 1;
while (i <= 5) {
	int j = 5;
	while (j >= i) {
		System.out.print("*");
		j--;
	}
System.out.println();
i++;
}

a)

*****

****

***

b)

*****

****

***

**

*

c)

*

**

***

d)

*

**

***

****

*****


👀 Answers to Practice Problems

  1. a
  2. d
  3. c
  4. c
  5. b

Key Terms to Review (7)

For Loop

: A for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times or until certain conditions are met.

Iteration

: Iteration refers to the process of repeating a set of instructions multiple times in order to achieve a desired outcome. It allows for efficient execution of repetitive tasks without having to write redundant code.

Nested For Loop

: A nested for loop is a loop structure where one loop is placed inside another loop. It allows for the repetition of a set of instructions within another set of instructions, resulting in more complex and controlled iterations.

System.out.println

: System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

Tracing

: Tracing refers to the process of following the execution of a program step by step to understand how it works and identify any errors or bugs.

Variables

: Variables are named storage locations in computer memory that hold values which can be changed during program execution.

while loop

: A while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.