324 lines
7.8 KiB
HTML
324 lines
7.8 KiB
HTML
<html>
|
|
<head>
|
|
<title>
|
|
C Guide--1.6 Statements
|
|
</title>
|
|
<!-- Changed by: eric huss, 12-Mar-1997 -->
|
|
</head>
|
|
<body text="#000000" bgcolor="#FFFFFF">
|
|
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="1.5.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
1.5 Operators</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="1.7.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
1.7 Preprocessing Directives</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
<hr>
|
|
|
|
|
|
<a name="if"></a>
|
|
<h1>1.6.1 if</h1>
|
|
<p>
|
|
The if statement evaluates an expression. If that expression is true, then a statement is
|
|
executed. If an else clause is given and if the expression is false, then the else's statement is
|
|
executed.
|
|
<p>
|
|
Syntax:<br>
|
|
<blockquote>
|
|
<code><b>if(</b></code> <i>expression</i> <code><b>)</b></code> <i>statement1</i><code><b>;</b></code><br>
|
|
<br>
|
|
<i>or</i><br>
|
|
<br>
|
|
<code><b>if(</b></code> <i>expression</i> <code><b>)</b></code> <i>statement1</i><code><b>;</b></code><br>
|
|
<code><b>else</b></code> <i>statement2</i> <code><b>;</b></code><br>
|
|
</blockquote>
|
|
<br>
|
|
Examples:<br>
|
|
<blockquote><code><b><pre>
|
|
|
|
if(loop<3) counter++;
|
|
|
|
if(x==y)
|
|
x++;
|
|
else
|
|
y++;
|
|
|
|
if(z>x)
|
|
{
|
|
z=5;
|
|
x=3;
|
|
}
|
|
else
|
|
{
|
|
z=3;
|
|
x=5;
|
|
}
|
|
</pre></b></code></blockquote>
|
|
|
|
<a name="switch"></a>
|
|
<h1> 1.6.2 switch</h1>
|
|
<p>
|
|
A switch statement allows a single variable to be compared with several possible
|
|
constants. If the variable matches one of the constants, then a execution jump is made to that
|
|
point. A constant can not appear more than once, and there can only be one default expression.<br>
|
|
<br>
|
|
Syntax:<br>
|
|
<blockquote><pre>
|
|
<code><b>switch (</b></code> <i>variable</i><code><b> )
|
|
{
|
|
case </b></code><i>const</i><code><b>:</b></code>
|
|
<i>statements...</i><code><b>;
|
|
default:
|
|
</b></code><i>statements...</i><code><b>;
|
|
}
|
|
</b></code>
|
|
</pre></blockquote>
|
|
Examples:<br>
|
|
<blockquote><code><b><pre>
|
|
|
|
switch(betty)
|
|
{
|
|
case 1:
|
|
printf("betty=1\n");
|
|
case 2:
|
|
printf("betty=2\n");
|
|
break;
|
|
case 3:
|
|
printf("betty=3\n");
|
|
break;
|
|
default:
|
|
printf("Not sure.\n");
|
|
}
|
|
</pre></b></code></blockquote>
|
|
|
|
If betty is 1, then two lines are printed: betty=1 and betty=2. If betty is 2, then only one line is
|
|
printed: betty=2. If betty=3, then only one line is printed: betty=3. If betty does not equal 1, 2,
|
|
or 3, then "Not sure." is printed.
|
|
<br>
|
|
<a name="while"></a>
|
|
<h1> 1.6.3 while</h1>
|
|
<p>
|
|
The while statement provides an iterative loop.
|
|
<br>
|
|
Syntax:<br>
|
|
<blockquote>
|
|
<code><b>while(</b></code> <i> expression</i> <code><b>)</b></code> <i> statement...</i>
|
|
</blockquote>
|
|
<i>statement</i> is executed repeatedly as long as <i>expression</i> is true. The test on <i>expression</i>
|
|
takes place before each execution of <i>statement</i>.
|
|
<br>
|
|
Examples:<br>
|
|
<blockquote><code><b><pre>
|
|
|
|
while(*pointer!='j') pointer++;
|
|
|
|
while(counter<5)
|
|
{
|
|
printf("counter=%i",counter);
|
|
counter++;
|
|
}
|
|
</pre></b></code></blockquote>
|
|
<br>
|
|
<a name="do"></a>
|
|
<h1> 1.6.4 do</h1>
|
|
<br>
|
|
The do...while construct provides an iterative loop.<br>
|
|
<br>
|
|
Syntax:<br>
|
|
<blockquote>
|
|
<code><b>do</code></b> <i>statement...</i> <code><b>while(</code></b> <i>expression</i> <code><b>);</code></b>
|
|
</blockquote>
|
|
<i>statement</i> is executed repeatedly as long as <i>expression</i> is true. The test on <i>expression</i>
|
|
takes place after each execution of <i>statement</i>.<br>
|
|
<br>
|
|
Examples:<br>
|
|
<blockquote><code><b><pre>
|
|
|
|
do {
|
|
betty++;
|
|
printf("%i",betty);
|
|
} while (betty<100);
|
|
|
|
</pre></b></code></blockquote>
|
|
<a name="for"></a>
|
|
<h1> 1.6.5 for</h1>
|
|
<p>
|
|
The for statement allows for a controlled loop.
|
|
<p>
|
|
Syntax:
|
|
<blockquote>
|
|
<code><b>for(</b></code> <i>expression1</i> ; <i>expression2</i> ; <i>expression3</i><code><b> )</b></code> <i>statement...</i>
|
|
</blockquote>
|
|
<i>expression1</i> is evaluated before the first iteration. After each iteration, <i>expression3</i> is
|
|
evaluated. Both <i>expression1</i> and <i>expression3</i> may be ommited. If <i>expression2</i> is ommited, it is
|
|
assumed to be 1. <i>statement</i> is executed repeatedly until the value of <i>expression2</i> is 0. The test
|
|
on <i>expression2</i> occurs before each execution of <i>statement</i>.
|
|
<p>
|
|
Examples:
|
|
<blockquote>
|
|
<code><b><pre>
|
|
for(loop=0;loop<1000;loop++)
|
|
printf("%i\n",loop);
|
|
</pre></b></code>
|
|
</blockquote>
|
|
Prints numbers 0 through 999.
|
|
<blockquote>
|
|
<code><b><pre>
|
|
for(x=3, y=5; x<100+y; x++, y--)
|
|
{
|
|
printf("%i\n",x);
|
|
some_function();
|
|
}
|
|
</pre></b></code>
|
|
</blockquote>
|
|
Prints numbers 3 through 53. some_function is called 51 times.
|
|
<br>
|
|
<a name="goto"></a>
|
|
<h1> 1.6.6 goto</h1>
|
|
<p>
|
|
The goto statement transfers program execution to some label within the program.
|
|
<p>
|
|
Syntax:
|
|
<blockquote>
|
|
<code><b>goto</b></code><i> label</i><code><b>;</b></code><br>
|
|
....<br>
|
|
<i>label</i><code><b>:</b></code><br>
|
|
</blockquote>
|
|
Examples:
|
|
<blockquote><code><b><pre>
|
|
goto skip_point;
|
|
printf("This part was skipped.\n");
|
|
skip_point:
|
|
printf("Hi there!\n");
|
|
</pre></b></code>
|
|
</blockquote>
|
|
Only the text "Hi there!" is printed.
|
|
<br>
|
|
<a name="continue"></a>
|
|
<h1> 1.6.7 continue</h1>
|
|
<p>
|
|
The continue statement can only appear in a loop body. It causes the rest of the
|
|
statement body in the loop to be skipped.
|
|
<p>
|
|
Syntax:
|
|
<blockquote><code><b>
|
|
continue;
|
|
</b></code></blockquote>
|
|
Examples:
|
|
<blockquote><code><b><pre>
|
|
for(loop=0;loop<100;loop++)
|
|
{
|
|
if(loop==50)
|
|
continue;
|
|
printf("%i\n",loop);
|
|
}
|
|
</pre></b></code>
|
|
</blockquote>
|
|
The numbers 0 through 99 are printed except for 50.
|
|
<blockquote>
|
|
<code><b><pre>
|
|
joe=0;
|
|
while(joe<1000)
|
|
{
|
|
for(zip=0;zip<100;zip++)
|
|
{
|
|
if(joe==500)
|
|
continue;
|
|
printf("%i\n",joe);
|
|
}
|
|
joe++;
|
|
}
|
|
</pre></b></code>
|
|
</blockquote>
|
|
Each number from 0 to 999 is printed 100 times except for the number 500 which is not printed
|
|
at all.
|
|
<a name="break"></a>
|
|
<h1> 1.6.8 break</h1>
|
|
<p>
|
|
The break statement can only appear in a switch body or a loop body. It causes the
|
|
execution of the current enclosing switch or loop body to terminate.
|
|
<p>
|
|
Syntax:
|
|
<blockquote><code><b>
|
|
break;
|
|
</b></code></blockquote>
|
|
Examples:
|
|
<blockquote><code><b><pre>
|
|
switch(henry)
|
|
{
|
|
case 1: print("Hi!\n");
|
|
break;
|
|
case 2: break;
|
|
}
|
|
</pre></b></code></blockquote>
|
|
If henry is equal to 2, nothing happens.
|
|
<blockquote><code><b><pre>
|
|
for(loop=0;loop<50;loop++)
|
|
{
|
|
if(loop==10)
|
|
break;
|
|
printf("%i\n",loop);
|
|
}
|
|
</pre></b></code></blockquote>
|
|
Only numbers 0 through 9 are printed.
|
|
<a name="return"></a>
|
|
<h1> 1.6.9 return</h1>
|
|
<p>
|
|
The return statement causes the current function to terminate. It can return a value to the
|
|
calling function. A return statement can not appear in a function whose return type is void. If
|
|
the value returned has a type different from that of the function's return type, then the value is
|
|
converted. Using the return statement without an expression creates an undefined result.
|
|
Reaching the } at the end of the function is the same as returning without an expression.
|
|
<p>
|
|
Syntax:
|
|
<blockquote><code><b>
|
|
return</b></code> <i> expression</i><code><b>;</b></code>
|
|
</blockquote>
|
|
Examples:
|
|
<blockquote><code><b><pre>
|
|
int alice(int x, int y)
|
|
{
|
|
if(x<y)
|
|
return(1);
|
|
else
|
|
return(0);
|
|
}
|
|
</pre></b></code></blockquote>
|
|
<hr>
|
|
|
|
<center>
|
|
<table border=0 width=100%>
|
|
<tr>
|
|
<td align=left width=20% valign=top>
|
|
<a href="1.5.html">
|
|
<img src="left.gif" border=0>
|
|
Previous Section<br>
|
|
1.5 Operators</a></td>
|
|
<td align=center width=60% valign=top>
|
|
| <a href="index.html">Table of Contents</a> |
|
|
<a href="index2.html">Index</a> |</td>
|
|
<td align=right width=20% valign=top>
|
|
<a href="1.7.html">
|
|
Next Section
|
|
<img src="right.gif" border=0><br>
|
|
1.7 Preprocessing Directives</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
</body>
|
|
</html>
|