Loop Exits and Structured Programming: Reopening the Debate Eric S
Total Page:16
File Type:pdf, Size:1020Kb
Loop Exits and Structured Programming: Reopening the Debate Eric S. Roberts Department of Computer Science Stanford University ABSTRACT 1. When the structure of a loop requires some preparation Internal exits from loops represent a critically important (such as reading an input value) before making the test control structure that should be taught in the introductory for termination, I encourage students to use the break CS1 curriculum. Without access to those facilities, students statement to force explicit exit from the interior of a are often incapable of solving simple programming loop. This strategy, which is discussed in Section 3, problems that occur frequently in applications. This paper allows students to solve what Dijkstra calls the loop- reviews the existing evidence in support of such facilities and-a-half problem in a way that appeals to the and argues that it is important to reconsider our traditional student’s intuition and avoids duplication of code. pedagogical approach as we adopt new languages of instruction. 2. In the context of a function, I encourage students to use the return statement as soon as the value of the 1. INTRODUCTION function becomes known, even if that return statement Twenty-five years ago, a fierce debate raged within the would force an early exit from a loop within the computer science community over the issue of structured function. Allowing return to be used in this way programming. At its essence, structured programming makes it much easier for students to solve a variety of represents a disciplined approach to software development problems, as illustrated in Section 4. based on abstraction, stepwise refinement, and a formal Although the response to my text has been quite favorable, approach to program correctness [Dahl72]. Unfortunately, the decision to allow internal loop exits in these restricted as the debate progressed, attention was focused less on the cases has elicited some negative response. One reviewer, general issues raised by structured programming than on a for example, wrote that using break “is akin to using the relatively small detail: whether or not it is ever appropriate proverbial goto.” Another charged that my approach to use the goto statement in a well-structured program. violates the basic tenets of structured programming. A Launched by a letter from Edsger Dijkstra in 1968 third declared that my use of break to exit from a while [Dijkstra68], the goto controversy raged back and forth loop is simply “unacceptable,” ruling out any further over the next several years, often resembling a religious consideration. war in emotional intensity. In the late 1970s, the debate The negative reactions expressed in such reviews died down, and a period of relative peace ensued. underscore the continued existence of controversy over The underlying controversy, however, has not gone what control structures are acceptable for academic use. away, and the question of which control structures are This paper summarizes the evidence supporting the use of acceptable remains a source of passionate concern. The internal loop exits and embedded return statements, and intensity of that concern has been demonstrated by some of examines the dangers associated with restricting students to the reactions I have received to the new CS1/CS2 a more tightly constrained control model. curriculum at Stanford and to my textbook, The Art and For many years, it was easy to discount the evidence in Science of C: A Library-Based Approach [Roberts95], favor of internal loop exits. As long as Pascal was which reflects the curricular revisions we have undertaken overwhelmingly accepted as the best language for teaching at Stanford. When we converted the curriculum from introductory programming, the issue had little practical Pascal to C [Roberts93], I decided—on the basis of many relevance. In Pascal, internal loop exits are simply not years of classroom experience—that we should move available, and those who adhere to the standard version of beyond the limited control structures available in Pascal the language are therefore forced to accept the control and allow students to exit from the interior of a loop in the structures that Pascal provides. following two cases: In the last few years, however, many schools have abandoned Pascal for more modern languages. For the Permission to copy without fee all or part of this material is most part, the new languages—even those that follow granted provided that the copies are not made or distributed directly in the Pascal tradition such as Modula-2 and Ada— for direct commercial advantage, the ACM copyright notice include both a structured mechanism for exiting from the and the title of the publication and its date appear, and notice is given that copying is by permission of the interior of a loop and an active return statement. The Association for Computing Machinery. To copy otherwise, availability of these facilities gives new relevance to the or to republish, requires a fee and/or specific permission. underlying pedagogical question of how to teach control SIGCSE ’95 3/95 Nashville, TN USA © 1995 ACM 0-89791-693-x/95/0003....$3.50 structures at the introductory level. It is time to reopen the indicates that transforming a conceptual image of a problem debate. into an approved control framework tends to introduce error or unnecessary complexity, it is important to examine that 2. SUFFICIENCY VERSUS PRACTICAL UTILITY framework to determine whether extensions are required. Before exploring the accumulated evidence that supports the use of internal loop exits, it is important to acknowledge 3. THE LOOP-AND-A-HALF PROBLEM that the control structures provided by Pascal have a sound The fact that some loop structures seem to have a natural theoretical basis. In their 1966 paper, Böhm and Jacopini exit in the middle has long been recognized. In his 1974 proved that it is possible to code any flowchart program comments on the goto controversy [Knuth74], Don Knuth using four control structures: atomic actions, sequential outlined his own perspective, as follows: composition, if-then-else, and while-do. These structures became known as D structures, after Edsger The iteration statements most often proposed as alternatives to goto statements have been “while B do Dijkstra, and were soon extended to form a larger set of S” and “repeat S until B”. However, in practice the control primitives called D′ structures [Ledgard75] that iterations I encounter very often have the form also includes the if-then, repeat-until, and case statements, as they exist in Standard Pascal. Thus, the A: S; statements provided by Pascal indeed constitute a sufficient if B then goto Z fi; set of control primitives, in the theoretical sense. T; The key point, however, is that theoretical sufficiency is goto A Z: not in itself the principal criterion for program language design. After all, the arithmetic if and goto statements where S and T both represent reasonably long sequences provided by Fortran II also constitute a sufficient set by this of code. If S is empty, we have a while loop, and if T definition, but no one would seriously advocate using those is empty we have a repeat loop, but in the general statements as the basis for control structures in this day and case it is a nuisance to avoid the goto statements. age. The discipline encouraged by the use of D structures A typical example of such an iteration occurs when S leads empirically to more reliable and more easily is the code to acquire or generate a new piece of data, B is the test for end of data, and T is the processing of that maintained programs than Fortran’s primitives support. data. Thus, although the evolution of D structures represented an important practical advance for programming language Loops of this sort, in which some processing must precede design, theoretical sufficiency was not the primary reason the test, come up quite often in programming and constitute for its success. If it were, language designers would have what Dijkstra has called the loop-and-a-half problem. The seen no need to augment the set of D structures with the canonical example of the loop-and-a-half problem is extremely useful D′ forms. precisely the one that Knuth describes: reading a set of A similar illustration can be drawn from the standard set input values until some sentinel value appears. If the basic of Boolean operators. Most languages define and, or, and strategy is expressed in pseudocode, the problem has the not as the primitive operations on Boolean data. If one’s following solution: primary concern is linguistic economy, this set could easily loop be considered wasteful, because the single operator nand read in a value; (or, equivalently, the operator nor) would be sufficient in if value = Sentinel then exit; theoretical terms. This theoretical result is critical for process the value hardware designers, who are able to exploit the sufficiency endloop; of a single operator to achieve greater regularity in the design of integrated circuits. For programmers, on the In this example, the loop/exit/endloop statement is an other hand, the fact that nand is by itself sufficient has little extension to the Pascal repertoire and represents a loop that practical relevance. The average applications programmer is terminated only by the execution of the exit statement in has no intuition regarding the behavior of nand and the interior of the loop body. The loop/exit/endloop therefore would not know how to use it effectively. extension makes it possible to express the algorithm so that Programming problems tend to be phrased in terms of the the input value is read in from the user, tested against the traditional logical connectives, and it is for this reason—the sentinel, and then processed as necessary.