M&C1: Values of variables

Understanding the change of values assigned to variables as a loop executes requires a student to abstract computation, the role of the variables, and their intended use. Students generally get a better understanding and build up confidence when tracing through the loop or seeing the code execute in a system like PythonTutor (http://pythontutor.com/). A key to success is to let students write and test their own loops.

All types of loops – for and while loops in Python – are driven by “loop control variables.”  A control variable changes its value and determines when the loop terminates. The control variable (or variables) of a while loop need to change their value inside the loop. If they don’t, the while loop will not terminate!  See infinite loops (M&C4) for more details.