【學習筆記】【C語言】迴圈結構-while,學習筆記結構-while

來源:互聯網
上載者:User

【學習筆記】【C語言】迴圈結構-while,學習筆記結構-while
1. 簡單使用

while ( 條件 )
{
    語句1;
    語句2;
    ....
}

如果條件成立,就會執行迴圈體中的語句(“迴圈體”就是while後面大括弧{}中的內容)。然後再次判斷條件,重複上述過程,直到條件不成立就結束while迴圈

while迴圈的特點:如果while中的條件一開始就不成立,那麼迴圈體中的語句永遠不會被執行.

 

可以省略大括弧{},但是只會影響到while後面的第一條語句。不建議省略大括弧。

while ( 條件 ) 
語句1;

2.代碼
 1 #include <stdio.h> 2  3 /* 4  if (條件) 5  { 6   7  } 8   9  while (條件)10  {11     迴圈體12  }13  14  運行原理15  1.如果一開始條件就不成立,永遠不會執行迴圈體16  2.如果條件成立,就會執行一次迴圈體,執行完畢,再次判斷條件是否成立......17  18  break19  直接結束整個while迴圈20  21  continue22  結束當前的迴圈體,進入下一次迴圈體的執行23  24  */25 26 int main()27 {28     // 1.先確定需要重複執行的操作29     30     // 2.再確定約束條件31     32     // 定義一個變數記錄做的次數33     int count = 0;34     35     /*36     while (count<50)37     {38         ++count;39         40         if (count%10 != 0)41         {42             printf("做第%d次伏地挺身\n", count);43         }44     }*/45     46     /*47     while (count<50)48     {49         ++count;50         51         if (count%10 == 0)52         {53             // 直接結束這一次迴圈體,進入下一次迴圈54             continue;55         }56         57         printf("做第%d次伏地挺身\n", count);58     }*/59     60     while (count < 50)61     {62         ++count;63         64         printf("做第%d次伏地挺身\n", count);65         66         if (count == 20)67         {68             break;69         }70     }71     72     73     return 0;74 }

練習

 1 /* 2  提示使用者輸入一個正整數n,計算1+2+3+…+n的和 3  */ 4  5 #include <stdio.h> 6  7 int main() 8 { 9     // 1.提示輸入10     printf("請輸入一個正整數:\n");11     12     // 2.接收輸入13     // 定義變數儲存使用者輸入的整數14     int n;15     scanf("%d", &n);16     17     if (n<=0)18     {19         printf("非法輸入\n");20         return 0;21     }22     23     // 3.計算24     // (1 + n) * n / 2;25     // 定義變數儲存和26     int sum = 0;27     int number = 0; // 預設被加的數值28     29     while (number < n)30     {31         number++;32         sum += number; // 累加33     }34     35     printf("%d\n", sum);36     37     return 0;38 }
 1 /* 2 題目:計算1~100中所有3的倍數的個數 3 */ 4  5 #include <stdio.h> 6  7 int main() 8 { 9     // 記錄3的倍數的個數10     int count = 0;11     12     // 記錄當前檢查的數值13     int number = 0;14     15     while (number < 100)16     {17         number++;18         19         // 說明number是3的倍數20         if (number%3 == 0)21         {22             count++;23         }24     }25     26     printf("1~100內3的倍數的個數:%d\n", count);27 }

3.注意點

 1 #include <stdio.h> 2  3 int main() 4 { 5     /* 6     while (10) 7     { 8         printf("哈哈哈哈\n"); 9     }*/10     11     /*12     int a = 10;13     // while (a>0); 死迴圈14     while (a>0)15     {16         a--;17         printf("哈哈哈\n");18     }*/19     20     // 最簡單的死迴圈21     //while(1);22     23     return 0;24 }

 

 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.