-
Notifications
You must be signed in to change notification settings - Fork 25
/
ch06s02.html
13 lines (12 loc) · 3.56 KB
/
ch06s02.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>2. do/while语句</title><link rel="stylesheet" href="styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><link rel="start" href="index.html" title="Linux C编程一站式学习" /><link rel="up" href="ch06.html" title="第 6 章 循环语句" /><link rel="prev" href="ch06s01.html" title="1. while语句" /><link rel="next" href="ch06s03.html" title="3. for语句" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2. do/while语句</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s01.html">上一页</a> </td><th width="60%" align="center">第 6 章 循环语句</th><td width="20%" align="right"> <a accesskey="n" href="ch06s03.html">下一页</a></td></tr></table><hr /></div><div class="sect1" lang="zh-cn" xml:lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2726570"></a>2. do/while语句</h2></div></div></div><p><code class="literal">do/while</code>语句的语法是:</p><div class="literallayout"><p>语句 → do 语句 while (控制表达式);</p></div><p><code class="literal">while</code>语句先测试控制表达式的值再执行循环体,而<code class="literal">do/while</code>语句先执行循环体再测试控制表达式的值。如果控制表达式的值一开始就是假,<code class="literal">while</code>语句的循环体一次都不执行,而<code class="literal">do/while</code>语句的循环体仍然要执行一次再跳出循环。其实只要有<code class="literal">while</code>循环就足够了,<code class="literal">do/while</code>循环和后面要讲的<code class="literal">for</code>循环都可以改写成<code class="literal">while</code>循环,只不过有些情况下用<code class="literal">do/while</code>或<code class="literal">for</code>循环写起来更简便,代码更易读。上面的<code class="literal">factorial</code>也可以改用<code class="literal">do/while</code>循环来写:</p><pre class="programlisting">int factorial(int n)
{
int result = 1;
int i = 1;
do {
result = result * i;
i = i + 1;
} while (i <= n);
return result;
}</pre><p>写循环一定要注意循环即将结束时控制表达式的临界条件是否准确,上面的循环结束条件如果写成<code class="literal">i < n</code>就错了,当<code class="literal">i == n</code>时跳出循环,最后的结果中就少乘了一个<code class="literal">n</code>。虽然变量名应该尽可能起得有意义一些,不过用<code class="literal">i</code>、<code class="literal">j</code>、<code class="literal">k</code>给循环变量起名是很常见的。</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s01.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s03.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">1. while语句 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 3. for语句</td></tr></table></div></body></html>