Computer Science 15-100, Fall 2008
Class Notes: Code Tracing
Code Tracing
1 class Foo { 2 public static void main(String[] args) { 3 int x = 3; 4 System.out.println("Now x = " + x); 5 x += 5; 6 System.out.println("And now x = " + x); 7 } 8 }
Trace:
Method: 'main' |
||
Line | Action | x |
2 | start | --- |
3 | set x to 3 | 3 |
4 | print "Now x = 3" | |
5 | set x to 8 | 8 |
6 | print "And now x = 8" | |
7 | return |
1 class Foo { 2 public static void main(String[] args) { 3 int x = 3; 4 System.out.println("Here x = " + x); 5 x += 5; 6 int y = 4; 7 System.out.println("And here x,y = " + x + "," + y); 8 } 9 }
Trace:
Method: 'main' |
|||
Line | Action | x | y |
2 | start | --- | --- |
3 | set x to 3 | 3 | |
4 | print "Here x = 3" | ||
5 | set x to 8 | 8 | |
6 | set y to 4 | 4 | |
7 | print "And here x,y = 8,4" | ||
8 | return |
1 class Foo { 2 public static void main(String[] args) { 3 int x = 3, y = 4; 4 System.out.println("Here x,y = " + x + "," + y); 5 y = 2 * (x = 5); 6 System.out.println("And here x,y = " + x + "," + y); 7 } 8 }
Trace:
Method: 'main' |
|||
Line | Action | x | y |
2 | start | --- | --- |
3 | set x to 3 | 3 | |
3 | set y to 4 | 4 | |
4 | print "Here x,y = 3,4" | ||
5 | set x = 5 | 5 | |
5 | set y = 10 | 10 | |
6 | print "And here x,y = 5,10" | ||
7 | return |
1 class Foo { 2 public static void main(String[] args) { 3 int i = -3; 4 if (i > 0) 5 System.out.println(i + " is positive"); 6 else if (i == 0) 7 System.out.println(i + " is zero"); 8 else 9 System.out.println(i + " is negative"); 10 } 11 }
Trace:
Method: 'main' |
||
Line | Action | i |
2 | start | --- |
3 | set i to -3 | -3 |
4 | test if (-3 > 0) | |
6 | test if (-3 == 0) | |
9 | print "-3 is negative" | |
10 | return |
1 class Foo { 2 public static void main(String[] args) { 3 String s = "ab"; 4 for (int i=0; i<s.length(); i++) 5 System.out.println(s.charAt(i)); 6 } 7 }
Trace:
Method: 'main' |
|||
Line | Action | s | i |
2 | start | --- | --- |
3 | set s to "abc" | "abc" | |
4 | set i to 0 | 0 | |
4 | test if (0 < 2) | ||
5 | print "a" | ||
4 | set i to 1 | 1 | |
4 | test if (1 < 2) | ||
5 | print "b" | ||
4 | set i to 2 | 2 | |
4 | test if (2 < 2) | ||
6 | return |
1 class Foo { 2 public static void main(String[] args) { 3 int x = 3, y = 4; 4 int lesser = Math.min(x,y); 5 System.out.println("lesser = " + lesser); 6 } 7 }
Trace:
Method: 'main' |
||||
Line | Action | x | y | lesser |
2 | start | --- | --- | --- |
3 | set x to 3 | 3 | ||
3 | set y to 4 | 4 | ||
4 | call Math.min(3,4) returns 3 |
|||
4 | set lesser to 3 | 3 | ||
5 | print "lesser = 3" | |||
6 | return |
Another Example:
1 class Foo { 2 public static void main(String[] args) { 3 int x = 3, y = 4; 4 int z = Math.min(x, (int)Math.sqrt(y)); 5 System.out.println("z = " + z); 6 } 7 }
Trace:
Method: 'main' |
||||
Line | Action | x | y | z |
2 | start | --- | --- | --- |
3 | set x to 3 | 3 | ||
3 | set y to 4 | 4 | ||
4 | call Math.sqrt(4) returns 2.0 |
|||
4 | call Math.min(3,2) returns 2 |
|||
4 | set z to 2 | 2 | ||
5 | print "z = 2" | |||
6 | return |
1 class Foo { 2 public static int times(int x, int y) { 3 return x*y; 4 } 5 6 public static void main(String[] args) { 7 int x = 3, y = 4; 8 int z = times(x,y); 9 System.out.println("z = " + z); 10 } 11 }
Trace:
Method: 'main' |
||||
Line | Action | x | y | z |
6 | start | --- | --- | --- |
7 | set x to 3 | 3 | ||
7 | set y to 4 | 4 | ||
8 | call times(3,4) |
|
8 | returns 12 | |||
8 | set z to 12 | 12 | ||
9 | print "z = 12" | |||
10 | return |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem