How to Design While Loops

Marco T. Morazán
(Seton Hall University)

Beginning students find the syntactic construct known as a while loop difficult to master. The difficulties revolve around guaranteeing loop termination and around learning how to properly sequence mutations to solve a problem. In fact, both of these are intertwined and students need to be taught a model that helps them reason about how to design while loops. For students that have been introduced to how to design programs using structural recursion, generative recursion, accumulative recursion, and mutation, the task of teaching them how to design while loops is made easier. These students are familiar, for example, with state variables, termination arguments, and accumulator invariants. All of these are fundamental in the design of while loops. This articles presents a novel technique used at Seton Hall University to introduce beginners to the design of while loops. It presents a design recipe that students can follow step-by-step to establish such things as the driver of the loop, the loop invariant, and the proper sequencing of mutations. The article also presents an example of designing a while-loop based function using the new design recipe.

In Jurriaan Hage: Proceedings Eighth and Ninth International Workshop on Trends in Functional Programming in Education (TFPIE 2019 and 2020), Vancouver, Canada and Krakow, Poland, 11th June 2019 and 12th February 2020, Electronic Proceedings in Theoretical Computer Science 321, pp. 1–18.
Published: 24th August 2020.

ArXived at: https://dx.doi.org/10.4204/EPTCS.321.1 bibtex PDF
References in reconstructed bibtex, XML and HTML format (approximated).
Comments and questions to: eptcs@eptcs.org
For website issues: webmaster@eptcs.org