精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦 > 學(xué)習(xí)英語(yǔ) > 專業(yè)英語(yǔ) > 計(jì)算機(jī)英語(yǔ) > c語(yǔ)言while的用法

      c語(yǔ)言while的用法

      時(shí)間: 長(zhǎng)思709 分享

      c語(yǔ)言while的用法

        while語(yǔ)句的一般形式為:while(表達(dá)式) 語(yǔ)句其中表達(dá)式是循環(huán)條件,語(yǔ)句為循環(huán)體。while語(yǔ)句的語(yǔ)義是:計(jì)算表達(dá)式的值,當(dāng)值為真(非0)時(shí), 執(zhí)行循環(huán)體語(yǔ)句。下面小編就為大家介紹下c語(yǔ)言while的用法。
        用while語(yǔ)句計(jì)算從1加到100的值。用傳統(tǒng)流程圖和N-S結(jié)構(gòu)流程圖表示算法,見(jiàn)圖:
        #include <stdio.h>
        int main(void){
        int i,sum=0;
        i=1;
        while(i<=100){
        sum=sum+i;
        i++;
        }
        printf("%d\n",sum);
        return 0;
        }
        統(tǒng)計(jì)從鍵盤輸入一行字符的個(gè)數(shù)。
        #include <stdio.h>
        int main(void){
        int n=0;
        printf("input a string:\n");
        while(getchar()!='\n') n++;
        printf("%d",n);
        return 0;
        }
        本例程序中的循環(huán)條件為getchar()!='\n',其意義是,,只要從鍵盤輸入的字符不是回車就繼續(xù)循環(huán)。循環(huán)體n++完成對(duì)輸入字符個(gè)數(shù)計(jì)數(shù)。從而程序?qū)崿F(xiàn)了對(duì)輸入一行字符的字符個(gè)數(shù)計(jì)數(shù)。
        使用while語(yǔ)句應(yīng)注意以下兩點(diǎn)。
        1) while語(yǔ)句中的表達(dá)式一般是關(guān)系表達(dá)或邏輯表達(dá)式,只要表達(dá)式的值為真(非0)即可繼續(xù)循環(huán)。
        #include <stdio.h>
        int main(void){
        int a=0,n;
        printf("\n input n:    ");
        scanf("%d",&n);
        while (n--) printf("%d  ",a++*2);
        return 0;
        }
        本例程序?qū)?zhí)行n次循環(huán),每執(zhí)行一次,n值減1。循環(huán)體輸出表達(dá)式a++*2的值。該表達(dá)式等效于(a*2; a++)。
        2) 循環(huán)體如包括有一個(gè)以上的語(yǔ)句,則必須用{}括起來(lái),組成復(fù)合語(yǔ)句。
      514999