正在加载图片...
1.1 Program Organization and Control Structures 11 Notice how we always indent the block of code that is acted upon by the control structure,leaving the structure itself unindented.Notice also our habit of putting the initial curly brace on the same line as the for statement,instead of on the next line. This saves a full line of white space,and our publisher loves us for it. IF structure.This structure in C is similar to that found in Pascal,Algol, FORTRAN and other languages,and typically looks like if(...) 4▣✉ 3 else if (...) 鱼 else g Since compound-statement curly braces are required only when there is more NUMERICAL RECIPES I than one statement in a block,however,C's if construction can be somewhat less ⊙ 令 explicit than the corresponding structure in FORTRAN or Pascal.Some care must be exercised in constructing nested if clauses.For example,consider the following: 1f(b>3) 1f(a>3)b+=1; else b -1; /*questionable!*/ As judged by the indentation used on successive lines,the intent of the writer of this code is the following:'If b is greater than 3 and a is greater than 3,then IENTIFIC increment b.If b is not greater than 3,then decrement b.'According to the rules 6 of C,however,the actual meaning is 'If b is greater than 3,then evaluate a.If a is greater than 3,then increment b,and if a is less than or equal to 3,decrement b.'The point is that an else clause is associated with the most recent open if statement 是 no matter how you lay it out on the page.Such confusions in meaning are easily resolved by the inclusion of braces.They may in some instances be technically superfluous;nevertheless,they clarify your intent and improve the program.The above fragment should be written as Numerica 10621 43106 1f(b>3)[ (outside Recipes if(a>3)b+=1; else b-=1; North Software. Here is a working program that consists dominantly of if control statements: #include <math.h> #def1 ne IGREG(15+31L*(10+12L*1582)) Gregorian Calendar adopted Oct.15,1582. long julday(int mm,int id,int iyyy) In this routine julday returns the Julian Day Number that begins at noon of the calendar date specified by month mm,day id,and year iyyy,all integer variables.Positive year signifies A.D.; negative,B.C.Remember that the year after 1 B.C.was 1 A.D. void nrerror(char error.-text☐);1.1 Program Organization and Control Structures 11 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). Notice how we always indent the block of code that is acted upon by the control structure, leaving the structure itself unindented. Notice also our habit of putting the initial curly brace on the same line as the for statement, instead of on the next line. This saves a full line of white space, and our publisher loves us for it. IF structure. This structure in C is similar to that found in Pascal, Algol, FORTRAN and other languages, and typically looks like if (...) { ... } else if (...) { ... } else { ... } Since compound-statement curly braces are required only when there is more than one statement in a block, however, C’s if construction can be somewhat less explicit than the corresponding structure in FORTRAN or Pascal. Some care must be exercised in constructing nested if clauses. For example, consider the following: if (b > 3) if (a > 3) b += 1; else b -= 1; /* questionable! */ As judged by the indentation used on successive lines, the intent of the writer of this code is the following: ‘If b is greater than 3 and a is greater than 3, then increment b. If b is not greater than 3, then decrement b.’ According to the rules of C, however, the actual meaning is ‘If b is greater than 3, then evaluate a. If a is greater than 3, then increment b, and if a is less than or equal to 3, decrement b.’ The point is that an else clause is associated with the most recent open if statement, no matter how you lay it out on the page. Such confusions in meaning are easily resolved by the inclusion of braces. They may in some instances be technically superfluous; nevertheless, they clarify your intent and improve the program. The above fragment should be written as if (b > 3) { if (a > 3) b += 1; } else { b -= 1; } Here is a working program that consists dominantly of if control statements: #include <math.h> #define IGREG (15+31L*(10+12L*1582)) Gregorian Calendar adopted Oct. 15, 1582. long julday(int mm, int id, int iyyy) In this routine julday returns the Julian Day Number that begins at noon ofthe calendar date specified by month mm, day id, and year iyyy, all integer variables. Positive year signifies A.D.; negative, B.C. Remember that the year after 1 B.C. was 1 A.D. { void nrerror(char error_text[]);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有