The computer is a very literal deviceit does exactly what we tell it to do, which may or may not be what we want it to do. We try to make sure that a program does what we want by tracing the execution of the statements.
We use a nonsense program below to demonstrate the technique. We keep track of the values of the program variables on the right-hand side. Variables with undefined values are indicated with a dash. When a variable is assigned a value, that value is listed in the appropriate column.
Value of
Statement
a
b
c
const int x = 5;
int main()
{
int a, b, c;
-
-
-
b = l;
-
1
-
c = x + b;
-
1
6
a = x + 4;
9
1
6
a = c;
6
1
6
b = c;
6
6
6
a = a + b + c;
18
6
6
c = c % x;
18
6
1
c = c * a;
18
6
18
a = a % b;
0
6
18
cout << a < b < c;
0
6
18
return 0;
0
6
18
}
Now that you've seen how the technique works, let's apply it to the LumberYard program. We list just the executable statement portion here, modified to reflect the results of our algorithm walk-through.