Pen and paper methodology
In the unit, we shall be doing a lot of work on computers, where your programs are auto-marked by the machine and instant feedback is given. However, one skill is critical for good understanding of programming and improving problem solving skills -
We will start with trace of some programs to establish what we expect students to do while tracing through code.
Tracing conditions
Trace the flow of the following program and determine the value of
resultat the end of it.Solution
a < bistrue
b % a == 0istrue
a % 2 == 0isfalseThe expression becomes
true && true && falseThis is
falseHence, the
elseblock executes andresultbecomesb (10).
Tracing conditions
Trace the flow of the following program and determine the value of
resultat the end of it.Solution
a == bisfalse,
elseexecutes
bdecreases by 5, becomes 5a == bistrue.ifblock executes andresultbecomesb (5).
Tracing condition and loop
Trace the flow of the following program and determine the value of
resultat the end of it.Solution
i i<=7 i%2 i%2==1 result 1 true 1 true -3+1 = -2 2 true 0 false 3 true 1 true -2+3 = 1 4 true 0 false 5 true 1 true 1+5 = 6 6 true 0 false 7 true 1 true 6+7 = 13 8 false
Tracing nested conditions
Trace the flow of the following code -
Solution
At the end of the code,
a = 5,b = 10,c = 2,d = false,result = 10. Explanation -