Tracing code is an excellent way to help you think in programming. By doing so you can see the processes of the program better, and you can check for bugs more easily. To trace code it is recommend that you have a physical copy of it in front of you, a pencil, and a sheet of scratch paper. (Faulty print outs make great scrap.)
Back to Functions and Libraries
cout << "Print variables"; int A, B; A = 5; B = 2; cout << "The sum is " << A+B; B *= 3 |
Print variableThe sum is 6 A: B: 2 If you couldn't tell, the left block is the example code, this is the scratch work, and the right is the thought process. |
|
int A = 4, B = 3; char ch = '4'; if ( A <= B ) ch = 't'; else { int c = 4 / b * 2 - 1; cout << c << B+A << endl; } cout << "A+B" << ch; |
17¶ A+B4 A: 4 B: 3 ch: 4 c: 1 |
|
void swap(int& A, int& B) { int temp = A; A = B; B = temp } void swap2(int A, int B) { int temp = A; A = B; B = temp } int f(int c, int v) { return c + c * v; } int main() { double g = 1.98, t = .09; int num = 3, x = 3, y = 1; cout << "Total" << f(g, t) << endl; swap(x, y); swap2(num, y) cout << num << ' ' << x << ' ' << y; } |
Total2.1582¶ 5 1 3 g: 1.98 t: .09 num: 5 x: y: f() c:1.98 v:.09 swap() A(ref): B(ref): temp: 3 swap2() A: B: temp: 3 |
Since this simple block involves a lot of calls, the explanation is below.
Back to Functions and Libraries
Prev -- Back to Portal -- Next