正在加载图片...
8 Chapter 1. Preliminaries Control Structures An executing program unfolds in time,but not strictly in the linear order in which the statements are written.Program statements that affect the order in which statements are executed,or that affect whether statements are executed.are called control statements.Control statements never make useful sense by themselves.They make sense only in the context of the groups or blocks of statements that they in turn control.If you think of those blocks as paragraphs containing sentences,then the control statements are perhaps best thought of as the indentation of the paragraph and the punctuation between the sentences,not the words within the sentences. 81 We can now say what the goal of structured programming is.It is to make program control manifestly apparent in the visual presentation of the program.You see that this goal has nothing at all to do with how the computer sees the program As already remarked,computers don't care whether you use structured programming or not.Human readers,however,do care.You yourself will also care,once you discover how much easier it is to perfect and debug a well-structured program than 以 one whose control structure is obscure. You accomplish the goals of structured programming in two complementary 乏 ways.First,you acquaint yourself with the small number of essential control structures that occur over and over again in programming,and that are therefore given convenient representations in most programming languages.You should learn to think about your programming tasks,insofar as possible,exclusively in terms of these standard control structures.In writing programs,you should get into the habit of representing these standard control structures in consistent,conventional ways. 9是 "Doesn't this inhibit creativity?"our students sometimes ask.Yes,just OF SCIENTIFIC as Mozart's creativity was inhibited by the sonata form,or Shakespeare's by the metrical requirements of the sonnet.The point is that creativity,when it is meant to communicate,does well under the inhibitions of appropriate restrictions on format. Second,you avoid,insofar as possible,control statements whose controlled blocks or objects are difficult to discern at a glance.This means,in practice,that you must try to avoid named labels on statements and goto's.It is not the goto's that are dangerous(although they do interrupt one's reading of a program);the named statement labels are the hazard.In fact,whenever you encounter a named statement Numerical Recipes 10621 label while reading a program,you will soon become conditioned to get a sinking 43108 feeling in the pit of your stomach.Why?Because the following questions will,by habit,immediately spring to mind:Where did control come from in a branch to this label?It could be anywhere in the routine!What circumstances resulted in a branch (outside to this label?They could be anything!Certainty becomes uncertainty,understanding Software. dissolves into a morass of possibilities North Some examples are now in order to make these considerations more concrete (see Figure 1.1.1). Catalog of Standard Structures Iteration. In C,simple iteration is performed with a for loop,for example for(j=2;j<=1000;j+){ b[j]=a[j-1]; a[j-1]=j;8 Chapter 1. Preliminaries Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Control Structures An executing program unfolds in time, but not strictly in the linear order in which the statements are written. Program statements that affect the order in which statements are executed, or that affect whether statements are executed, are called control statements. Control statements never make useful sense by themselves. They make sense only in the context of the groups or blocks of statements that they in turn control. If you think of those blocks as paragraphs containing sentences, then the control statements are perhaps best thought of as the indentation of the paragraph and the punctuation between the sentences, not the words within the sentences. We can now say what the goal of structured programming is. It is to make program control manifestly apparent in the visual presentation of the program. You see that this goal has nothing at all to do with how the computer sees the program. As already remarked, computers don’t care whether you use structured programming or not. Human readers, however, do care. You yourself will also care, once you discover how much easier it is to perfect and debug a well-structured program than one whose control structure is obscure. You accomplish the goals of structured programming in two complementary ways. First, you acquaint yourself with the small number of essential control structures that occur over and over again in programming, and that are therefore given convenient representations in most programming languages. You should learn to think about your programming tasks, insofar as possible, exclusively in terms of these standard control structures. In writing programs, you should get into the habit of representing these standard control structures in consistent, conventional ways. “Doesn’t this inhibit creativity?” our students sometimes ask. Yes, just as Mozart’s creativity was inhibited by the sonata form, or Shakespeare’s by the metrical requirements of the sonnet. The point is that creativity, when it is meant to communicate, does well under the inhibitions of appropriate restrictions on format. Second, you avoid, insofar as possible, control statements whose controlled blocks or objects are difficult to discern at a glance. This means, in practice, that you must try to avoid named labels on statements and goto’s. It is not the goto’s that are dangerous (although they do interrupt one’s reading of a program); the named statement labels are the hazard. In fact, whenever you encounter a named statement label while reading a program, you will soon become conditioned to get a sinking feeling in the pit of your stomach. Why? Because the following questions will, by habit, immediately spring to mind: Where did control come from in a branch to this label? It could be anywhere in the routine! What circumstances resulted in a branch to this label? They could be anything! Certainty becomes uncertainty, understanding dissolves into a morass of possibilities. Some examples are now in order to make these considerations more concrete (see Figure 1.1.1). Catalog of Standard Structures Iteration. In C, simple iteration is performed with a for loop, for example for (j=2;j<=1000;j++) { b[j]=a[j-1]; a[j-1]=j; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有