Loop Exits and Structured Programming:
Reopening the Debate
Eric S. Roberts
Department of Computer Science
Stanford University
NOTE
This paper has been submitted to the Twenty-sixth SIGCSE Technical
Symposium on Computer Science Education. Copyright is retained by the
authors prior to publication release.
ABSTRACT
Internal exits from loops represent a critically important control
structure that should be taught in the introductory CS1 curriculum.
Without access to those facilities, students are often incapable of
solving simple programming problems that occur frequently in
applications. This paper reviews the existing evidence in support of
such facilities and argues that it is important to reconsider our
traditional pedagogical approach as we adopt new languages of
instruction.
1. INTRODUCTION
Twenty-five years ago, a fierce debate raged within the computer science
community over the issue of structured programming. At its essence,
structured programming represents a disciplined approach to software
development based on abstraction, stepwise refinement, and a formal
approach to program correctness [Dahl72]. Unfortunately, as the debate
progressed, attention was focused less on the general issues raised by
structured programming than on a relatively small detail: whether or not
it is ever appropriate to use the goto statement in a well-structured
program. Launched by a letter from Edsger Dijkstra in 1968
[Dijkstra68], the goto controversy raged back and forth over the next
several years, often resembling a religious war in emotional intensity.
In the late 1970s, the debate died down, and a period of relative peace
ensued.
The underlying controversy, however, has not gone away, and the question
of which control structures are acceptable remains a source of
passionate concern. The intensity of that concern has been demonstrated
by some of the reactions I have received to the new CS1/CS2 curriculum
at Stanford and to my textbook, The Art and Science of C: A Library-
Based Approach [Roberts95], which reflects the curricular revisions we
have undertaken at Stanford. When we converted the curriculum from
Pascal to C [Roberts93], I decided -- on the basis of many years of
classroom experience -- that we should move beyond the limited control
structures available in Pascal and allow students to exit from the
interior of a loop in the following two cases:
1. When the structure of a loop requires some preparation (such
as reading an input value) before making the test for
termination, I encourage students to use the break statement
to force explicit exit from the interior of a loop. This
strategy, which is discussed in Section 3, allows students to
solve what Dijkstra calls the loop-and-a-half problem in a way
that both appeals to the student's intuition and avoids
duplication of code.
2. In the context of a function, I encourage students to use the
return statement as soon as the value of the function becomes
known, even if that return statement would force an early exit
from a loop within the function. Allowing return to be used
in this way makes it much easier for students to solve a
variety of problems, as illustrated in Section 4.
Although the response to my text has been quite favorable, the decision
to allow internal loop exits in these restricted cases has elicited some
negative response. One reviewer, for example, wrote that using break
"is akin to using the proverbial goto." Another charged that my
approach violates the basic tenets of structured programming. A third
declared that my use of break to exit from a while loop is simply
"unacceptable," ruling out any further consideration.
The negative reactions expressed in such reviews underscore the
continued existence of controversy over what control structures are
acceptable for academic use. This paper summarizes the evidence
supporting the use of internal loop exits and embedded return
statements, and examines the dangers associated with restricting
students to a more tightly constrained control model.
For many years, it was easy to discount the evidence in favor of
internal loop exits. As long as Pascal was overwhelmingly accepted as
the best language for teaching introductory programming, the issue had
little practical relevance. In Pascal, internal loop exits are simply
not available, and those who adhere to the standard version of the
language are therefore forced to accept the control structures that
Pascal provides.
In the last few years, however, many schools have abandoned Pascal for
more modern languages. For the most part, the new languages -- even
those that follow directly in the Pascal tradition such as Modula-2 and
Ada -- include both a structured mechanism for exiting from the interior
of a loop and an active return statement. The availability of these
facilities gives new relevance to the underlying pedagogical question of
how to teach control structures at the introductory level. It is time
to reopen the debate.
2. SUFFICIENCY VERSUS PRACTICAL UTILITY
Before exploring the accumulated evidence that supports the use of
internal loop exits, it is important to acknowledge that the control
structures provided by Pascal have a sound theoretical basis. In their
1966 paper, Boehm and Jacopini proved that it is possible to code any
flowchart program using four control structures: atomic actions,
sequential composition, if-then-else, and while-do. These structures
became known as D structures, after Edsger Dijkstra, and were soon
extended to form a larger set of control primitives called D' structures
[Ledgard75] that also includes the if-then, repeat-until, and case
statements as they exist in Standard Pascal. Thus, the statements
provided by Pascal indeed constitute a sufficient set of control
primitives, in the theoretical sense.
The key point, however, is that theoretical sufficiency is not in itself
the principal criterion for program language design. After all, the
arithmetic if and goto statements provided by Fortran II also constitute
a sufficient set by this definition, but no one would seriously advocate
using those statements as the basis for control structures in this day
and age. The discipline encouraged by the use of D structures leads
empirically to more reliable and more easily maintained programs than
Fortran's primitives support. Thus, although the evolution of D
structures represented an important practical advance for programming
language design, theoretical sufficiency was not the primary reason for
its success. If it were, language designers would have seen no need to
augment the set of D structures with the extremely useful D' forms.
A similar illustration can be drawn from the standard set of Boolean
operators. Most languages define and, or, and not as the primitive
operations on Boolean data. If one's primary concern is linguistic
economy, this set could easily be considered wasteful, because the
single operator nand (or, equivalently, the operator nor) would be
sufficient in theoretical terms. This theoretical result is critical
for hardware designers, who are able to exploit the sufficiency of a
single operator to achieve greater regularity in the design of
integrated circuits. For programmers, on the other hand, the fact that
nand is by itself sufficient has little practical relevance. The
average applications programmer has no intuition regarding the behavior
of nand and therefore would not know how to use it effectively.
Programming problems tend to be phrased in terms of the traditional
logical connectives, and it is for this reason -- the admittedly
subjective criterion of "naturalness" in the sense of being consistent
with intuition -- that and, or, and not form the basis of Boolean
calculation.
The question of wh
没有合适的资源?快使用搜索试试~ 我知道了~
The art and science of C(C语言的科学和艺术)-源代码
共249个文件
c:136个
h:54个
makefile:19个
需积分: 2 16 下载量 37 浏览量
2010-06-03
00:10:16
上传
评论 1
收藏 829KB ZIP 举报
温馨提示
The art and science of C(C语言的科学和艺术)-源代码 The art and science of 斯坦福大学编的,国外很多大学作为C语言的教材,很有深度
资源推荐
资源详情
资源评论
收起资源包目录
The art and science of C(C语言的科学和艺术)-源代码 (249个子文件)
install.bat 1KB
graphics.c 46KB
graphics.c 46KB
graphics.c 44KB
xdisplay.c 27KB
graphics.c 17KB
xmanager.c 11KB
teach.c 8KB
strlib.c 5KB
strlib.c 5KB
strlib.c 5KB
strlib.c 5KB
strlib.c 5KB
strlib.c 5KB
strlib.c 5KB
calendar.c 5KB
house.c 5KB
graphics.c 5KB
graphics.c 5KB
graphics.c 5KB
mileage2.c 4KB
countlet.c 4KB
simpio.c 4KB
simpio.c 4KB
simpio.c 4KB
simpio.c 4KB
simpio.c 4KB
genlib.c 4KB
genlib.c 4KB
genlib.c 4KB
genlib.c 4KB
genlib.c 4KB
genlib.c 4KB
reverse.c 4KB
simpio.c 3KB
simpio.c 3KB
craps.c 3KB
mileage.c 3KB
exceptio.c 3KB
exception.c 3KB
exception.c 3KB
exceptio.c 3KB
exception.c 3KB
exceptio.c 3KB
piglatin.c 3KB
empdb.c 3KB
nsqrt.c 3KB
remcom.c 3KB
tsqrt.c 3KB
invert.c 2KB
testsort.c 2KB
testsort.c 2KB
testsort.c 2KB
xcompat.c 2KB
linecopy.c 2KB
sort.c 2KB
sort.c 2KB
testrev.c 2KB
primes3.c 2KB
permute.c 2KB
employee.c 2KB
queue.c 2KB
sort.c 2KB
primes2.c 2KB
copyfile.c 2KB
scanner.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
random.c 2KB
findcoin.c 2KB
elements.c 2KB
combine.c 1KB
repfirst.c 1KB
ucfile.c 1KB
protect.c 1KB
acronym.c 1KB
balance4.c 1KB
drawcbox.c 1KB
hours.c 1KB
qtest.c 1KB
power.c 1KB
genlib.c 1KB
ncopies.c 1KB
fact.c 1KB
c2ftable.c 1023B
point.c 1008B
primes1.c 1000B
balance3.c 982B
balance1.c 972B
fact.c 970B
euclid.c 938B
balance2.c 926B
lastchar.c 891B
reverse.c 880B
gcd.c 870B
共 249 条
- 1
- 2
- 3
资源评论
swordsman_zds
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功