Understanding of loop constructs and loop comprehension can be tested by giving students short pieces of code containing loops and asking whether they are identical. Students should do so without running programs!
Below are four loop constructs. Are they all producing the same output? If not, which one of choice1 to choice3 has the same output as original?
#original for k in range(1,100): if k % 4 == 0: print(k) |
#choice 1 for k in range(1,25): print k |
#choice 2 k = 1 while k <= 100: print k k = k + 4 |
#choice 3 k = 4 while k < 100: print k k = k + 4 |
In this example, choice3 produces the same result. Simple exercises like this one test comprehension of loops as well as flow of execution. Similar questions are typically given on the Java AP exam.