⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 6 flow of control -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 3 页
字号:
is really just an embellished while in which the three operations normally 
performed on loop-control variables (initialize, test, and modify) are brought 
together syntactically. It has the form </FONT></P>
<UL>
  <P><CODE>for ( ExpressionList? ;<BR>ExpressionList? ;<BR>ExpressionList? ) 
  Statement</CODE></P></UL>
<P><FONT face="Times New Roman,Times">For statements are performed in the 
following steps: </FONT></P>
<P><FONT face="Times New Roman,Times">The first ExpressionList is evaluated. 
This is done only once to initialize the control variable(s). </FONT></P>
<P><FONT face="Times New Roman,Times">The second ExpressionList is evaluated to 
determine whether or not to perform Statement. If more than one expression is 
given, the right-most expression yields the value to be tested. If it yields 
false (zero), control passes on to whatever follows the for statement. But, if 
it yields true (non-zero), Statement executes. </FONT></P>
<P><FONT face="Times New Roman,Times">The third ExpressionList is then evaluated 
to adjust the control variable(s) for the next pass, and the process goes back 
to step 2. E.g.,</FONT></P>
<UL>
  <P><CODE>for(J=100;J&lt;1000;J++) { process();}</CODE></P></UL>
<P><FONT face="Times New Roman,Times"><IMG height=214 
src="Chapter 6 Flow of Control -- Valvano.files/FLOW35.GIF" 
width=234></FONT></P>
<P><FONT face="Times New Roman,Times">A five-element array is set to zero, could 
be written as </FONT></P>
<UL>
  <P><CODE>for (i = 4; i &gt;= 0; --i) array[i] = 0; </CODE></P></UL>
<P><FONT face="Times New Roman,Times">or a little more efficiently as 
</FONT></P>
<UL>
  <P><CODE>for (i = 5; i; array[--i] = 0) ; </CODE></P></UL>
<P><FONT face="Times New Roman,Times">Any of the three expression lists may be 
omitted, but the semicolon separators must be kept. If the test expression is 
absent, the result is always true. Thus </FONT></P>
<UL>
  <P><CODE>for (;;) {...break;...} </CODE></P></UL>
<P><FONT face="Times New Roman,Times">will execute until the break is 
encountered. </FONT></P>
<P><FONT face="Times New Roman,Times">As with the <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#WHILE">while</A> 
statement, break and continue statements may be used with equivalent effects. A 
break statement makes control jump directly to whatever follows the for 
statement. And a continue skips whatever remains in the controlled block so that 
the third ExpressionList is evaluated, after which the second one is evaluated 
and tested. In other words, a continue has the same effect as transferring 
control directly to the end of the block controlled by the for. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=DO></A>The Do 
Statement</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The do statement is the third loop 
controlling statement in C. It is really just an execute-first while statement. 
It has the form </FONT></P>
<UL>
  <P><CODE>do Statement while ( ExpressionList ) ;</CODE></P></UL>
<P><FONT face="Times New Roman,Times">Statement is any simple or compound 
statement. The do statement executes in the following steps: </FONT></P>
<P><FONT face="Times New Roman,Times">Statement is executed. </FONT></P>
<P><FONT face="Times New Roman,Times">Then, ExpressionList is evaluated and 
tested. If more than one expression is given, the right most expression yields 
the value to be tested. If it yields true (non-zero), control goes back to step 
1; otherwise, it goes on to whatever follows. </FONT></P>
<P><FONT face="Times New Roman,Times">As with the while and for statements, 
break and continue statements may be used. In this case, a continue causes 
control to proceed directly down to the while part of the statement for another 
test of ExpressionList. A break makes control exit to whatever follows the do 
statement. </FONT></P>
<UL>
  <P><CODE>I=100; do { process(); I--;} while (I&gt;0);</CODE><FONT 
  face="Monaco,Courier New" size=1> </FONT></P></UL>
<P><FONT face="Monaco,Courier New" size=1><IMG height=214 
src="Chapter 6 Flow of Control -- Valvano.files/FLOW34.GIF" 
width=200></FONT></P>
<P><FONT face="Times New Roman,Times">The example of the five-element array 
could be written as </FONT></P>
<UL>
  <P><CODE>i = 4;<BR>do {array[i] = 0; --i;} while (i &gt;= 0); </CODE></P></UL>
<P><FONT face="Times New Roman,Times">or as </FONT></P>
<UL>
  <P><CODE>i = 4;<BR>do array[i--] = 0; while (i &gt;= 0); </CODE></P></UL>
<P><FONT face="Times New Roman,Times">or as </FONT></P>
<UL>
  <P><CODE>i = 5;<BR>do array[--i] = 0; while (i); </CODE></P></UL>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=RETURN></A>The Return 
Statement</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The return statement is used within a 
function to return control to the caller. Return statements are not always 
required since reaching the end of a function always implies a return. But they 
are required when it becomes necessary to return from interior points within a 
function or when a useful value is to be returned to the caller. Return 
statements have the form </FONT></P>
<UL>
  <P><CODE>return ExpressionList? ; </CODE></P></UL>
<P><FONT face="Times New Roman,Times">ExpressionList? is an optional list of 
expressions. If present, the last expression determines the value to be returned 
by the function. I f absent, the returned value is unpredictable. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=NULL></A>Null 
Statements</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The simplest C statement is the null 
statement. It has no text, just a semicolon terminator. As its name implies, it 
does exactly nothing. Why have a statement that serves no purpose? Well, as it 
turns out, statements that do nothing can serve a purpose. As we saw in <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm">Chapter 5</A>, 
expressions in C can do work beyond that of simply yielding a value. In fact, in 
C programs, all of the work is accomplished by expressions; this includes 
assignments and calls to functions that invoke operating system services such as 
input/output operations. It follows that anything can be done at any point in 
the syntax that calls for an expression. Take, for example, the statement 
</FONT></P>
<DIR>
<P><CODE>while ((SCSR &amp; TDRE) == 0); /* Wait for TDRE to be set */ 
</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">in which the 
</FONT><CODE>(SCSR&amp;TDRE)==0)</CODE><FONT face="Times New Roman,Times"> 
controls the execution of the null statement following. The null statement is 
just one way in which the C language follows a philosophy of attaching intuitive 
meanings to seemingly incomplete constructs. The idea is to make the language as 
general as possible by having the least number of disallowed constructs. 
</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=GOTO></A>The Goto 
Statement</FONT></I></B></P>
<P><B><FONT face="Times New Roman,Times">Goto</FONT></B><FONT 
face="Times New Roman,Times"> statements break the sequential flow of execution 
by causing control to jump abruptly to designated points. They have the general 
form <B>goto Name </B>where <B>Name</B> is the name of a label which must appear 
in the same function. It must also be unique within the function. </FONT></P>
<P><CODE>int data[10];<BR>void clear(void){ int n; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n=1; <BR>loop: 
data[n]=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(n==10) 
goto done;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto loop; 
<BR>done:<BR>}</CODE></P>
<P><I><FONT face=Times>Listing 6.6: Examples of a goto statements</FONT></I></P>
<P><FONT face="Times New Roman,Times">Notice that labels are terminated with a 
colon. This highlights the fact that they are not statements but statement 
prefixes which serve to label points in the logic as targets for goto 
statements. When control reaches a <B>goto</B>, it proceeds directly from there 
to the designated label. Both forward and backward references are allowed, but 
the range of the jump is limited to the body of the function containing the 
<B>goto</B> statement. </FONT></P>
<P><FONT face="Times New Roman,Times">As we observed above, goto statements, 
cannot be used in functions which declare locals in blocks which are subordinate 
to the outermost block of the function. </FONT></P>
<P><FONT face="Times New Roman,Times">Because they violate the structured 
programming criteria, <B>goto</B> statements should be used sparingly, if at 
all. Over reliance on them is a sure sign of sloppy thinking. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=MISSING></A>Missing 
Statements</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">It may be surprising that nothing was said 
about input/output, program control, or memory management statements. The reason 
is that such statements do not exist in the C language proper. </FONT></P>
<P><FONT face="Times New Roman,Times">In the interest of portability these 
services have been relegated to a set of standard functions in the run-time 
library. Since they depend so heavily on the run-time environment, removing them 
from the language eliminates a major source of compatibility problems. Each 
implementation of C has its own library of standard functions that perform these 
operations. Since different compilers have libraries that are pretty much 
functionally equivalent, programs have very few problems when they are compiled 
by different compilers.</FONT></P>
<P><FONT face="Times New Roman,Times">Go to <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">Chapter 7 on 
Pointers</A> Return to <A 
href="http://www.ece.utexas.edu/~valvano/embed/toc1.htm">Table of Contents</A> 
</FONT></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -