C----循環(huán)

C----循環(huán)

在C語言中循環(huán)可分為3中,while循環(huán)、do - while循環(huán)、for 循環(huán)。


一、while循環(huán)

  while即:首先檢查啟動循環(huán)的條件是否滿足,當(dāng)條件滿足時,不斷地重復(fù)循環(huán)體內(nèi)的語句,直到不滿足條件就退出。

  while循環(huán)基本形式:

while(條件){? ? ? <循環(huán)體>;

<循環(huán)體>;

}

 eg: 1

//輸入任意個int型,整數(shù)并判斷其位數(shù)#include int main()

{

? ? int x;

? ? intn=0;

? ? scanf("%d",&x);

? ? n++;

? ? x /=10;

? ? while(x >0){

? ? ? ? n++;

? ? ? ? x /=10;

? ? }

? ? printf("%d\n",n);

? ? return0;

}

eg: 2

//判斷輸入的位數(shù)/*1.用戶輸入x;

2.初始化n為0;

3.x = x/10,去掉個位;

4.n++;

5.如果 x>0,回到3;

6.否則n就是結(jié)果; */

#include int main()

{

? ? int x;

? ? intn=0;

? scanf("%d",&x);? ? n++;//n=1x /=10;//對輸入的數(shù)進(jìn)行取整數(shù)商,得到的 x放入while循環(huán)中 while(x >0){//判斷 x 是否大于零,滿足條件則執(zhí)行大括號中的語句? n++;//n=2x /=10;

? ? }

? ? printf("%d\n",n);

? ? return0;

}

eg: 3

//輸入一個數(shù),計算階乘(while方法)

#include int main()

{

? ? intn;//定義變量n為輸入的數(shù) scanf("%d",&n);//讀入輸入的數(shù) intfact =1;//定義變量fact保存程序的結(jié)果 (初始化) inti =1;//定義變量 i 作為循環(huán)條件 (初始化) while(i <= n){

? ? ? ? fact *= i;

? ? ? ? i ++;

? ? }

? ? printf("%d!=%d\n",n,fact);

? ? return0;

}


二、do while 循環(huán)

  do while 即:進(jìn)入do while 前不做檢查,而是在執(zhí)行完一輪循環(huán)后再來檢查循環(huán)的條件是否滿足,如果滿足則繼續(xù)下一輪循環(huán),不滿足則結(jié)束循環(huán)。

do while 循環(huán)至少是要被執(zhí)行一次的!

  基本形式:

do

{<循環(huán)體>;<循環(huán)體>;

}while (<條件>);? ? ? //在while大括號后面必須要有括號


while循環(huán)和do while 循環(huán)的區(qū)別:while循環(huán)是先判斷條件, do while 循環(huán)是在執(zhí)行了一輪循環(huán)后再來判斷條件,無論條件是否滿足do while 都至少會被執(zhí)行一輪,

而while循環(huán)是條件滿足執(zhí)行,不滿足結(jié)束循環(huán)。

?eg:?

//判斷任意位數(shù)(do -while方法)#include int main()

{

? ? int x;

? ? intn=0;

? ? scanf("%d",&x);

? ? do? ? {

? ? ? ? x /=10;

? ? ? ? n ++;

? ? } while( x >0);

? ? printf("%d\n",n);

? ? return0;

}


三、for 循環(huán)

  for循環(huán)類似一個計數(shù)循環(huán),設(shè)定一個計數(shù)器,初始化它,然后在計數(shù)器到達(dá)某值之前重復(fù)執(zhí)行循環(huán)體,同時每執(zhí)行一輪循環(huán),計數(shù)器值以一定步進(jìn)進(jìn)行調(diào)整。

for(初始動作,條件,每輪的動作){

<循環(huán)體>;

<循環(huán)體>;

}

小套路:做求和程序時,記錄結(jié)果的變量應(yīng)該初始化為0;而做求積的 程序時,記錄結(jié)果的變量應(yīng)該初始化為1.


while循環(huán)和for 循環(huán)可以說是等價的,任何一個for循環(huán)都可以改造成while循環(huán)

//while循環(huán)和for 循環(huán)可以說是等價的,任何一個for循環(huán)都可以改造成while循環(huán)for(inti=1, i <=n, i ++){

? ? fact? *= i;

}

? ? ? ? ? ? |||||||? ? ? ? ? ? |||||||inti =1;while( i <=n ){

? ? fact *=i;

? ? i ++;

}

eg:

//輸入一個數(shù),計算階乘(for方法) #include int main()

{

? ? int n;

? ? scanf("%d",&n);

? ? intfact =1;


? ? inti =1;//定義變量 i 初始值為1 for(i=1; i<=n; i++ ){

? ? ? ? //i=1作為初始條件 ,i<=n是循環(huán)繼續(xù)的條件,i++是 循環(huán)每一輪要做的事情 /* 例如我前面輸入的變量 n 等于4,當(dāng)執(zhí)行到 for 循環(huán)時。首先計算機(jī)會將變量 i=1 作為初始值

? ? ? ? 如果 i 小于等于4就執(zhí)行循環(huán),即執(zhí)行循環(huán)體的語句塊,最后再來執(zhí)行 i++, 反之就跳出循環(huán)*/

? ? ? ? fact *= i;

? ? }

? ? printf("%d!=%d\n",n,fact);

? ? return0;

}


總結(jié):如果程序循環(huán)的次數(shù),有固定次數(shù)用for;

   如果必須執(zhí)行一次,用do - while;

  ? ?其它情況用while;

C/C++學(xué)習(xí)交流群:1083020561

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容