You are on page 1of 7

How to Perform a Code Trace Code tracing is a form of algorithm analysis in which you (the analyst) simulate computer

operation. You are not determining the efficiency of any given algorithm, but you are evaluating the logic. You step through each line of code, determining what the execution of that line causes to happen. When I teach students to perform a code trace, I am very prescriptive. f course, if you are familiar with another approach that wor!s, you may feel free to follow that approach. "ere, however, is a sure fire way to perform the code trace process. #o that we have something specific to tal! about, we will use the following code snippet$ X = 1; Y = 2; Z = 3; A = 0; While Z > A X = X * 2; Y = Y 1; Pri!t "Y#PP$$%%%& Z = Z ' 1; $()WH#*$ #tep ne$ %abel every line of code %ine &umber ' ( ) * + , . / '0 %ine of Code X = 1; Y = 2; Z = 3; A = 0; While Z > A X = X * 2; Y = Y 1; Pri!t "Y#PP$$%%%& Z = Z ' 1; $()WH#*$

#tep 1wo$
2epartment of Information and Computer #cience I343I Copyright (00,

Create a table that has a column for every variable referenced in your code, 4%3# one more. (In our case, x,y,5, a 6 ' more 7 + columns). 4lan for as many rows as there are lines of code. If the code includes branching, you might need fewer rows8 if the code includes looping, you might need more rows. 1he table for the provided code snippet would loo! as follows$ %ine 9 ' ( ) * + , . / '0 #tep 1hree #tep through the code, one line at a time. >nter into the chart whatever executing that line causes to happen. :ar ; :ar Y :ar < :ar = #ystem Impact

%ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +) , (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0)

:ar ; '

:ar Y ?

:ar < ?

:ar = ?

#ystem Impact @

&ote '$ =s a result of executing the first line of code (; 7 '), the value stored at variable location ; is set to '. You donAt !now anything about the other variable locations, and there wasnAt any system impact.

2epartment of Information and Computer #cience I343I Copyright (00,

%ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +) , (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0)

:ar ; ' '

:ar Y ? (

:ar < ? ?

:ar = ? ?

#ystem Impact @ @

&ote ($ =s a result of executing line (, the value of Y is set to (. ; remains unchanged, you !now nothing about the other variables, and there is no system impact. %etAs speed things up a bit$ %ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +) , (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0) :ar ; ' ' ' ' :ar Y ? ( ( ( :ar < ? ? ) ) :ar = ? ? ? 0 #ystem Impact @ @ @ @

&ote )$ < set to ) &ote *$ = set to 0 &ow something a little more interesting begins to happen. We hit the conditional in line +. Bemember, a conditional is some expression that can be evaluated to a Coolean (ie, 1rue or Dalse) We will evaluate the conditional, listing the evaluation in the #ystem Impact column.

2epartment of Information and Computer #cience I343I Copyright (00,

%ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +)

:ar ; ' ' ' '

:ar Y ? ( ( (

:ar < ? ? ) )

:ar = ? ? ? 0

, (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0)

( ( ( (

( ) ) )

) ) ) (

0 0 0 0

#ystem Impact @ @ @ @ (< E =) (is ) E 0) ? 1B3> Foto line , instead of line '0 @ @ GYI44>>HHHI prints @

&ote +$ Conditional is evaluated. Currently, the evaluation is$ is ) E 0. f course, it is, so we proceed to line ,. 1he driving variable in a conditional, in this case, <, is often referred to as a GsentinelI value. 1he name comes from the observation that the variable stands li!e a sentry or guard in front of the code bloc! (here, a while loop). If the conditional evaluates to true, you are allowed to pass inside the loop. If the conditional evaluates to false, you must bypass the bloc! and proceed to the next executeable line of code. 1he Gmagic fairiesI spit lines of code, loo!ing for some signal that they have reached the end of the bloc!. In this case, they loo! for the !eyword >&2W"I%>, and then start in on the very next line of executable code that follows it. (In our case, there is no more code). &ote ,$ ; is multiplied by (. #o, ' J ( 7 ( &ote - $ ' is added to Y. #o, ( 6 ' 7 ) &ote .$ 1he text string GYI44>>HHHI, without the Kuotation mar!s, of course. &ote /$ ' is subtracted from <, so ) L ' 7 ( =nother interesting moment. %ine '0 contains a !eyword, G>&2W"I%>I. It mar!s the end of a W"I%> coding bloc!. 1he magic fairies have ta!en &)0', and they !now to go bac! up to the conditional on %ine +, and test to see if it is still true$

2epartment of Information and Computer #cience I343I Copyright (00,

%ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +)

:ar ; ' ' ' '

:ar Y ? ( ( (

:ar < ? ? ) )

:ar = ? ? ? 0

, (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0)

( ( ( (

( ) ) )

) ) ) (

0 0 0 0

#ystem Impact @ @ @ @ (< E =) (is ) E 0) ? 1B3> Foto line , instead of line '0 @ @ GYI44>>HHHI prints @ Cloc! end Fo1o %ine +

&ote '0$ 2esignates end of While bloc!, sends execution flow bac! to %ine +, where the conditional will be re@evaluated. %ine 9 ' (&ote ') ( (&ote () ) (&ote )) * (&ote *) + (&ote +) :ar ; ' ' ' ' :ar Y ? ( ( ( :ar < ? ? ) ) :ar = ? ? ? 0 #ystem Impact @ @ @ @ (< E =) (is ) E 0) ? 1B3> Foto line , instead of line '0 @ @ GYI44>>HHHI prints @ Cloc! end Fo1o %ine + (< E =) (is ( E 0)? 1B3>

, (&ote ,) - (&ote -) . (&ote .) / (&ote /) '0 (&ote '0) + (&ote '')

( ( ( ( (

( ) ) ) )

) ) ) ( (

0 0 0 0 0

2epartment of Information and Computer #cience I343I Copyright (00,

, (&ote '() - (&ote ')) . (&ote '*) / (&ote '+) '0 (&ote ',)

* * * * *

) * * * *

( ( ( ' '

0 0 0 0 0

+ (&ote '-)

'

, (&ote '.) - (&ote '/) . (&ote (0) / (&ote (') '0 (&ote (()

. . . . .

* + + + +

' ' ' 0 0

0 0 0 0 0

+ (&ote ())

'0 (&ote (*)

Foto line , @ @ GYI44>>HHHI prints @ >nd of While Cloc!, goto line + (<E=) (is ' E 0)? 1B3> Foto line , @ @ GYI44>>HHHI prints @ >nd of While Cloc!, goto line + (<E=) (is 0E0)? D=%#> Foto line '0 WeAre 2oneHH

&ote ''$ Conditional is still true, so execution goes bac! to , &ote '($ ; J ( so ( J ( 7 * &ote ')$ Y 6 ', so ) 6 ' 7 * &ote '* $ GYI44>>HHHI prints &ote '+$ <@', so ( L ' 7 ' &ote ',$ >ndwhile, return to + to reevaluate conditional &ote '-$ Conditional ' E 08 still true, proceed to line , &ote '.$ ; J ( so * J ( 7 . &ote '/ $ Y 6 ', so * 6 ' 7 + &ote (0$ GYI44>>HHHI prints &ote ('$ < L ', so ' L ' 7 0 &ote (($ >ndwhile, return to line + to reevaluate conditional &ote ()$ Conditional 0 E 08 D=%#>, so loo! for >&2W"I%> &ote (*$ =fter finding the !eyword (here, >&2W"I%>), flow goes to next executable line

2epartment of Information and Computer #cience I343I Copyright (00,

#ome time in his or her life, every respectable nerd (ie, any graduating &)0' studentH) has performed a code trace. When code traces appear on exams, here are typical Kuestions$ What is the ending value of Y? What printed on the screen, if anything, and how many times? What would happen if instead of decreasing Z, line 9 increased Z? 1he code snippets are rarely long (cu5 who wants to grade them?) =nd, be of good cheerM if you follow the process outlined above, you can answer code snippet Kuestions =ll 2ay %ong. "ope this helpsH

2epartment of Information and Computer #cience I343I Copyright (00,

You might also like