📄 chapter 6 flow of control -- valvano.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm -->
<HTML><HEAD><TITLE>Chapter 6: Flow of Control -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META
content="Power HD:Applications:Microsoft Office 98:Templates:Web Pages:Blank Web Page"
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT
face=Helvetica,Arial size=4>Chapter 6: Flow of Control</FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 6?</FONT></I></B></P>
<DIR>
<P><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#SIMPLE">Simple
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#COMPOUND">Compound
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#IF">if and
if-else statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#SWITCH">switch
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#WHILE">while
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#FOR">for
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#DO">do
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#RETURN">return
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#GOTO">goto
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#NULL">Null
statements</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap6/chap6.htm#MISSING">Missing
statements</A></P></DIR>
<P><FONT face="Times New Roman,Times">Every procedural language provides
statements for determining the flow of control within programs. Although
declarations are a type of statement, in C the unqualified word statement
usually refers to procedural statements rather than declarations. In this
chapter we are concerned only with procedural statements. </FONT></P>
<P><FONT face="Times New Roman,Times">In the C language, statements can be
written only within the body of a function; more specifically, only within
compound statements. The normal flow of control among statements is sequential,
proceeding from one statement to the next. However, as we shall see, most of the
statements in C are designed to alter this sequential flow so that algorithms of
arbitrary complexity can be implemented. This is done with statements that
control whether or not other statements execute and, if so, how many times.
Furthermore, the ability to write compound statements permits the writing a
sequence of statements wherever a single, possibly controlled, statement is
allowed. These two features provide the necessary generality to implement any
algorithm, and to do it in a structured way. </FONT></P>
<P><FONT face="Times New Roman,Times"><A name=SIMPLE></A></FONT><B><I><FONT
face=Helvetica,Arial>Simple Statements</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The C language uses semicolons as
statement terminators. A semicolon follows every simple (non-compound)
statement, even the last one in a sequence. </FONT></P>
<P><FONT face="Times New Roman,Times">When one statement controls other
statements, a terminator is applied only to the controlled statements. Thus we
would write </FONT></P>
<UL>
<P><CODE>if(x > 5) x = 0; else ++x;</CODE><FONT
face="Times New Roman,Times"> </FONT></P></UL>
<P><FONT face="Times New Roman,Times">with two semicolons, not three. Perhaps
one good way to remember this is to think of statements that control other
statements as "super" statements that "contain" ordinary (simple and compound)
statements. Then remember that only simple statements are terminated. This
implies, as stated above, that compound statements are not terminated with
semicolons. Thus </FONT></P>
<UL>
<P><CODE>while(x < 5) {func(); ++x;}</CODE></P></UL>
<P><FONT face="Times New Roman,Times">is perfectly correct. Notice that each of
the simple statements within the compound statement is terminated. </FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=COMPOUND></A>Compound
Statements</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The terms compound statement and block
both refer to a collection of statements that are enclosed in braces to form a
single unit. Compound statements have the form </FONT></P>
<UL>
<P><CODE>{ObjectDeclaration?... Statement?... }</CODE></P></UL>
<P><FONT face="Times New Roman,Times">ObjectDeclaration?... is an optional set
of local declarations. If present, C requires that they precede the statements;
in other words, they must be written at the head of the block. Statement?... is
a series of zero or more simple or compound statements. Notice that there is not
a semicolon at the end of a block; the closing brace suffices to delimit the
end. In this example the local variable </FONT><CODE>temp</CODE><FONT
face="Times New Roman,Times"> is only defined within the inner compound
statement.</FONT></P>
<P><CODE>void main(void){ int n1,n2; <BR> n1=1; n2=2;
<BR> { int temp;
<BR> temp=n1; n1=n2; n2=temp; /* switch
n1,n2 */<BR> } <BR>}</CODE></P>
<P><I><FONT face=Times>Listing 6.1: Examples of a compound
statements</FONT></I></P>
<P><FONT face="Times New Roman,Times">The power of compound statements derives
from the fact that one may be placed anywhere the syntax calls for a statement.
Thus any statement that controls other statements is able to control units of
logic of any complexity. </FONT></P>
<P><FONT face="Times New Roman,Times">When control passes into a compound
statement, two things happen. First, space is reserved on the stack for the
storage of local variables that are declared at the head of the block. Then the
executable statements are processed. </FONT></P>
<P><FONT face="Times New Roman,Times">One important limitation in C is that a
block containing local declarations must be entered through its leading brace.
This is because bypassing the head of a block effectively skips the logic that
reserves space for local objects. Since the goto and switch statements (below)
could violate this rule. </FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=IF></A>The If
Statement</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">If statements provide a non-iterative
choice between alternate paths based on specified conditions. They have either
of two forms </FONT></P>
<UL>
<P><CODE>if ( ExpressionList ) Statement1</CODE></P></UL>
<P><FONT face="Times New Roman,Times">or </FONT></P>
<UL>
<P><CODE>if ( ExpressionList ) Statement1<BR>else Statement2 </CODE></P></UL>
<P><FONT face="Times New Roman,Times">ExpressionList is a list of one or more
expressions and Statement is any simple or compound statement. First,
ExpressionList is evaluated and tested. If more than one expression is given,
they are evaluated from left to right and the right-most expression is tested.
If the result is true (non-zero), then the Statement1 is executed and the
Statement2 (if present) is skipped. If it is false (zero), then Statement1 is
skipped and Statement2 (if present) is executed. In this first example, the
function </FONT><FONT face="Monaco,Courier New" size=1>isGreater()</FONT><FONT
face="Times New Roman,Times"> is executed if G2 is larger than 100.</FONT></P>
<UL>
<P><CODE>if(G2 > 100) isGreater();</CODE></P></UL>
<P><FONT face="Times New Roman,Times"><IMG height=123
src="Chapter 6 Flow of Control -- Valvano.files/FLOW1.GIF" width=235></FONT></P>
<ADDRESS><FONT face="Times New Roman,Times">Figure 6.1: Example if
statement.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">A 3-wide median filter can be designed
using if-else conditional statements. </FONT></P>
<P><CODE>int Median(int u1,int u2,int u3){ int
result;<BR> if(u1>u2)<BR> if(u2>u3) result=u2; //
u1>u2,u2>u3 u1>u2>u3<BR> else<BR> if(u1>u3) result=u3; //
u1>u2,u3>u2,u1>u3 u1>u3>u2<BR> else result=u1; //
u1>u2,u3>u2,u3>u1 u3>u1>u2<BR> else
<BR> if(u3>u2) result=u2; //
u2>u1,u3>u2 u3>u2>u1<BR> else<BR> if(u1>u3) result=u3; //
u2>u1,u2>u3,u1>u3 u2>u1>u3<BR> else result=u1; //
u2>u1,u2>u3,u3>u1 u2>u3>u1<BR> return(result):}</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 6.2: A 3-wide median
function.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">For more information on the design and
analysis of digital filters, see Chapter 15 of <U>Embedded Microcomputer
Systems: Real Time Interfacing</U> by Jonathan W. Valvano. </FONT></P>
<P><FONT face="Times New Roman,Times">Complex conditional testing can be
implemented using the <A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#RELATIONAL">relational</A>
and <A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#BOOLEAN">boolean</A>
operators described in the last chapter.</FONT></P>
<UL>
<P><CODE>if ((G2==G1)||(G4>G3)) True(); else False();</CODE></P></UL>
<P><FONT face="Times New Roman,Times"><IMG height=123
src="Chapter 6 Flow of Control -- Valvano.files/FLOW28.GIF"
width=362></FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=SWITCH></A>The Switch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -