5.2 Trace Tables
5.2 Trace Tables
LECTURE OBJECTIVES
Be able to use Trace Tables to view variable values
The message variable (string variable)
Learn how to display output in a MessageBox
Trace Tables
Trace tables can be used to test an algorithm to make
sure that the algorithm is free of logical errors
x y z
5 3 4
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
7
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
7
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
7 4
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
7 4
Trace Tables – Example:
Use a trace table to determine what the values of x, y, and z
will be once the code below has been executed.
x y z
5 3 4
7 4 11
Trace Tables - Class Exercise:
Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
a b c
0 1 5
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
1
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
1
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
1 7
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
1 7
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
a b c
0 1 5
1 7 3
Order of Precedence
Operator Precedence:
the rules that determine the order in which parts of a
mathematical expression are evaluated.
Evaluation is from
left to right
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = (c * b) – a;
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = (c * b) – a;
20 = (3 * 7) - 1
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = c * b – a;
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
will be onceWhat would
the code belowthe
hasvalue of
been executed.
answer be?
answer = c * b – a;
20 = 3 * 7 - 1
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
What
will be once thewould the value
code below of executed.
has been answer
be?
ans = a + c * b + b / 2 - a;
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
ans= a + c * b + b / 2 - a;
24 = 1 + 3 * 7 + 7 / 2 – 1
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
What would the value of answer
Use a trace table to determine what the values of a, b, and c
be?
will be once the code below has been executed.
ans= a + c * b + b / 2 - a;
24 = 1 + 3 * 7 + 7 / 2 – 1
24 = 1 + 21 + 3 - 1
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
answer = b * c / 2;
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
What would the value of answer
Use a trace table to determine what the values of a, b, and c
be?
will be once the code below has been executed.
answer = b * c / 2;
10 = 7 * 3 / 2
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace table to determine what the values of a, b, and c
What
will be once thewould the has
code below value ofexecuted.
been answer
be?
answer = b / c * 2;
a b c
All variables are
0 1 5
integers
1 7 3
Trace Tables - Class Exercise Solution:
Use a trace
What would
table the value
to determine what of
theanswer
values of a, b, and c
will be once the code below has been executed.
be?
answer = b / c * 2;
4 = 7 / 3 * 2
a b c
All variables are
0 1 5
integers
1 7 3
Trace Table Exercise
Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
int a = 10, b = 5, c = 3; a b c
a = b + c * 2;
b = b + (10 - c);
c = b + b;
Trace Table Solution
Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
Trace Table Exercise
Use a trace table to determine what the values of a, b,
and c will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
3 5 10
a = 3; b = 5; c = 10;
a = a * b;
b = c + 7 * a / 2;
a = b;
a = b / 10;
Trace Table Exercise
Use a trace table to determine what the values of a, b, and c
will be once the code below has been executed.
int a = 10, b = 5, c = 3;
a = b + c * 2; a b c
b = b + (10 - c); 10 5 3
11 12 24
c = b + b;
3 5 10
15 62
a = 3; b = 5; c = 10; 62
a = a * b; 6
b = c + 7 * a / 2;
a = b;
a = b / 10;