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

📄 c语言基础(14-循环控制01.htm

📁 语言基础(初学都必修)包括数据类型、运算符与表达式,运算符和表达式,逻辑运算符和逻辑表达式,C语言中的关键字,定义宏和使用宏定义,数据类型转换
💻 HTM
📖 第 1 页 / 共 2 页
字号:
		<table cellpadding="0" cellspacing="0" width="96%" height="100%">
			<!-- MSTableType="layout" -->
			<tr>
				<td height="17" valign="top">
				<p align="left">C语言程序控制结构有三种,分别是 1)顺序&nbsp; 2)分支&nbsp; 3)循环&nbsp; 这里讲一讲循环控制结构。<BR>在C语言程序中,大量地使用了循环结构。我们看一看下面的简单例子,以了解为什么要用循环控制:<BR>这个程序要完成这样一个任务,对一个变量计数,从1一直计到1000为止。如果不用循环,程序如下:<BR>void main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp; int i=1;<BR>&nbsp;&nbsp;&nbsp; i=i+1;<BR>&nbsp;&nbsp;&nbsp; i=i+1;<BR>&nbsp;&nbsp;&nbsp; i=i+1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;....<BR>&nbsp;&nbsp;&nbsp; ....&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*第999个i=i+1*/<BR>}<BR><BR>&nbsp;&nbsp;&nbsp; 以上程序所完成的任务是很简单的,一直加到变量i的值为1000为止。由于是顺序结构的程序,这样每次加1。总共要写i=i+1这样的语句多少次,要写999次。如果所要求i的终值更大,需要写的程序代码就更多。<BR>&nbsp;&nbsp;&nbsp; 分析一下上面的程序,可以看到程序语句重复的规律,在进行变量i的增值运算时,用的都是同一条语句i=i+1,也就是i++;实际上这样的重复代码可以由“循环结构”中的语句来自动完成。<BR>&nbsp;&nbsp;&nbsp; 循环控制语句有三种: 1)while 循环语句&nbsp;&nbsp;&nbsp; 2) do... while 循环语句&nbsp;&nbsp; 3)for循环语句。下面对这三种循环语句进行详细的讲解。<BR><BR>1、while 语句<BR>while语句的一般形式:<BR>while(condition)<BR>&nbsp;&nbsp;&nbsp; statement;<BR>&nbsp;&nbsp;&nbsp; statement可以是一个空语句,单语句或一个复合语句。condition可以是任何一种有效的表达式,当该表达式逻辑值为真时,循环重复执行,否则程序控制将转向循环代码后面的语句。<BR>&nbsp;&nbsp; 下面用wile循环语句把上面的程序重写一下:<BR>#include&lt;stdio.h&gt;<BR>void main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp; int i=1;<BR>&nbsp;&nbsp;&nbsp; while(i&lt;=1000)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*当变量i的值小于等于1000时循环,否则中止循环*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*循环执行的语句,当i小于等于1000时执行这条语句,共执行999次*/<BR>&nbsp;&nbsp;&nbsp; printf("%d",i);<BR>}<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; 可以看到,虽然同样进行了多次运算,但执行语句却只有二条:while语句和i++语句。所以有了循环语句,可以大大地减少程序语句的数量。( 但是要注意,程序语句减少了,程序的执行效率并没有因为采用了循环语句而提高了,因为程序在实际运行时,为了i的值加到1000,同样的要运行那么多的次数。程序的编写效率与执行效率是两回事)<BR>&nbsp;&nbsp;&nbsp; while循环在进入循环前测试条件,如果刚开始i的值已经大于1000,那么循环代码一次也不会执行:<BR>&nbsp;&nbsp;&nbsp;&nbsp;i=1001;<BR>&nbsp;&nbsp;&nbsp; while(i&lt;1000)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*进入循环前测试条件,这里条件是i&lt;=1000*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*这一条语句一次也没有执行*/<BR><BR><FONT color=#000000>下面是一个while循环的例子,这个程序按一定的格式输出255个ASCII字符:<BR>#include&lt;stdio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://包/"><FONT color=#000000><U>包</U></FONT></A><FONT color=#000000>含标准输入输出库函数*/<BR>#include&lt;conio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://包/"><FONT color=#000000><U>包</U></FONT></A><FONT color=#000000>含控制台输入输出函数*/<BR>void main(void)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://主/"><FONT color=#000000><U>主</U></FONT></A><FONT color=#000000>函数 (无返回值)*/<BR>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://主/"><FONT color=#000000><U>主</U></FONT></A><FONT color=#000000>函数开始*/<BR>&nbsp; &nbsp;int j=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://定/"><FONT color=#000000><U>定</U></FONT></A><FONT color=#000000>义整型变量 j*/<BR>&nbsp; &nbsp;unsigned char i=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://定/"><FONT color=#000000><U>定</U></FONT></A><FONT color=#000000>义无符号字符型变量 i*/<BR>&nbsp; &nbsp;clrscr();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://清/"><FONT color=#000000><U>清</U></FONT></A><FONT color=#000000>除屏幕*/<BR>&nbsp;&nbsp; printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://输/"><FONT color=#000000><U>输</U></FONT></A><FONT color=#000000>出空格--光标定位用*/<BR>&nbsp;&nbsp; while( i&lt;255 )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*while循环开始</FONT><FONT color=#000000>*/<BR>&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT color=#000000><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!(i=='\n')&amp;&amp;(!(i=='\t'))&amp;&amp;(!(i=='\r'))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://if/"><FONT color=#000000><U>if</U></FONT></A><FONT color=#000000> 选择语句*/<BR>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&amp;&amp;(!(i=='\a'))&amp;&amp;(!(i=='\b'))&amp;&amp;(!(i=='\v')))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(" %c",i);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://如/"><FONT color=#000000><U>如</U></FONT></A><FONT color=#000000>果if表达式的值成立就以字符形式输出i的值*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else printf(" ");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://否/"><FONT color=#000000><U>否</U></FONT></A><FONT color=#000000>则就输出一个空格*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(j)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://如/"><FONT color=#000000><U>如</U></FONT></A><FONT color=#000000>果 j 的值不为 0*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!(j%12))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</FONT><A href="file://如/"><FONT color=#000000><U>如</U></FONT></A><FONT color=#000000>果 j 能被 12 除尽*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; printf("\n");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*</FONT><A href="file://换/"><FONT color=#000000><U>换</U></FONT></A><FONT color=#000000>行*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ");&nbsp; /*</FONT><A href="file://输/"><FONT color=#000000><U>输</U></FONT></A><FONT color=#000000>出空格--光标定位用*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* if 选择语句结束*/<BR>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* if 选择语句结束*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++;j++;<BR>&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT> </td>
			</tr>
		</table>
		</div>
		</td>
    </tr>
    <tr>
      <td width=758 bgColor=#b1bde0>
      <P align=right>教程录入:Sunpeople&nbsp;&nbsp;&nbsp;&nbsp;责任编辑:Sunpeople&nbsp;  </P></td>
    </tr>
    <tr>
      <td height=16>
		<form method="POST" action="--WEBBOT-SELF--">
			<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
			<p align="left">&nbsp;&nbsp;&nbsp; <li>上一个教程: <a class='LinkPrevArticle' href='/c/ShowArticle.asp?ArticleID=32' title='文章标题:C语言基础(09-有符号数的形式)
作&nbsp;&nbsp;&nbsp;&nbsp;者:dd
更新时间:2006-6-6 18:08:05'>C语言基础(09-有符号数的形式)</a></li><BR>&nbsp;&nbsp;&nbsp; <li>下一个教程: <a class='LinkNextArticle' href='/c/ShowArticle.asp?ArticleID=68' title='文章标题:循环语句中的break和continue有何区别?
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-6-9 8:57:02'>循环语句中的break和continue有何区别?</a></li></p>
		</form>
		</td>
    </tr>
       <tr>
      <td width=758 bgColor=#adb9dd>##相关推荐:</td>
    </tr>
    <tr>
      <td width=758>
<div align=right>
        <table cellSpacing=0 cellPadding=0 width="99%">
        <!-- MSTableType="layout" -->
          <tr>
            <td vAlign=top height=118><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1422' title='文章标题:06.9全国计算机等级考试二级C语言上机题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:32:49' target="_self">06.9全国计算机等级考试二级…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1421' title='文章标题:06.9全国计算机等级考试二级C语言上机题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:32:04' target="_self">06.9全国计算机等级考试二级…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1420' title='文章标题:06.9全国计算机等级考试二级C语言上机题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:31:17' target="_self">06.9全国计算机等级考试二级…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1419' title='文章标题:06.9全国计算机等级考试二级C语言上机题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:30:27' target="_self">06.9全国计算机等级考试二级…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1418' title='文章标题:06.9全国计算机等级考试二级C语言上机题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:21:25' target="_self">06.9全国计算机等级考试二级…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1417' title='文章标题:2006年9月23日计算机等级考试二级C上机试题
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:19:24' target="_self">2006年9月23日计算机等级考试…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1416' title='文章标题:全国计算机等级考试二级C语言上机题2
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 17:16:40' target="_self">全国计算机等级考试二级C语言…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1415' title='文章标题:全国计算机等级考试二级C语言上机题1
作&nbsp;&nbsp;&nbsp;&nbsp;者:未知
更新时间:2006-11-4 16:58:55' target="_self">全国计算机等级考试二级C语言…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1192' title='文章标题:C语言教程第二章:&nbsp;数据类型、运算符、表达式
作&nbsp;&nbsp;&nbsp;&nbsp;者:佚名
更新时间:2006-9-29 17:26:28' target="_self">C语言教程第二章:&nbsp;数据类型、…</a><br><a class='LinkArticleCorrelative' href='/c/ShowArticle.asp?ArticleID=1191' title='文章标题:C语言教程第二章:&nbsp;数据类型、运算符、表达式
作&nbsp;&nbsp;&nbsp;&nbsp;者:佚名
更新时间:2006-9-29 17:25:13' target="_self">C语言教程第二章:&nbsp;数据类型、…</a><br></td>
          </tr>
        </table>
      </div></td>
    </tr>
  </table>
  <table height=164 cellSpacing=0 cellPadding=0 width=760 border=0>
    <tr>
      <td width=760 background=/images1/index_32.gif height=26>
      <P align=center>| <A class=Bottom onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://lsjs.gsau.edu.cn');" href="#">设为首页</A> | <A class=Bottom href="javascript:window.external.addFavorite('http://lsjs.gsau.edu.cn','蓝色极速');">加入收藏</A> | <A class=Bottom href="mailto:wxw404@gmail.com">联系站长</A> | <A class=Bottom href="/FriendSite/Index.asp" target=_blank>友情链接</A> | <A class=Bottom href="/Copyright.asp" target=_blank>版权申明</A> | </P></td>
    </tr>
    <tr>
      <td width=760 background=/images1/index_33.gif height=138>
      <P align=center>Copyright (C) 蓝色极速 All Rights Reserved<BR>建议使用:1024*768分辨率,16位以上颜色、Netscape6.0<BR>IE5.0以上版本浏览器和中文大字符集</P></td>
    </tr>
  </table>
</body>
</html>




⌨️ 快捷键说明

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