天天看點

【程式設計語言-C++】第九講 C++ 循環語句while、for

【程式設計語言-C++】第九講 C++ 循環語句while、for
【程式設計語言-C++】第九講 C++ 循環語句while、for
【程式設計語言-C++】第九講 C++ 循環語句while、for

前言

【程式設計語言-C++】第九講 C++ 循環語句while、for

1、上節課回顧

【程式設計語言-C++】第九講 C++ 循環語句while、for

上一次給大家講了選擇語句,選擇語句包括判斷語句和開關語句。選擇語句結構比較簡單,也容易應用,重點在于多寫代碼,多練習,熟能生巧。

大家在寫的時候,可能比較穩重,利用switch-case語句時候,每一個都是分開寫的;利用if-else語句用了很多邏輯或,甚至直接使用if-else if-else格式。上面這種方法,找到了月份與日期對應的規律。

【程式設計語言-C++】第九講 C++ 循環語句while、for

2、上節思考題答案

【程式設計語言-C++】第九講 C++ 循環語句while、for

1.思考題1:

今年是2019年,請利用if語句和switch語句分别實作如下功能:

輸入月份,輸出該月對應的天數。

//switch-case語句
#include<iostream>

using namespace std;

void main() {

int month;
cout << "please input month which in 2019 :";
cin >> month;

switch (month)
        {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout << "本月有31天" << endl;
break;
case 4:
case 6:
case 9:
case 11:
cout << "本月有30天" << endl;
break;
default:
cout << "本月有28天" << endl;
break;
        }

    }

//if-else語句

#include<iostream>

using namespace std;

void main() {
int month;
cout << "please input month which in 2019 :";
cin >> month;

if (month<8&&(month % 2)|| month>8&& (month % 2 == 0))
cout << "本月有31天" << endl;
else if (month == 2)
cout << "本月有28天" << endl;
else
cout << "本月有30天" << endl;

    }      

      大家在寫的時候,可能比較穩重,利用switch-case語句時候,每一個都是分開寫的;利用if-else語句用了很多邏輯或,甚至直接使用if-else if-else格式。上面這種方法,找到了月份與日期對應的規律。

這個是比較簡單的應用數學的例子,能夠使代碼更加簡潔,甚至簡化計算過程。是以,掌握好數學,有助于我們更好地學習程式設計。是以希望大家能夠好好學習數學,打好數學基礎。

2.思考題2:

利用if語句和switch語句分别實作簡單電腦:

計算兩個浮點型資料的加減乘除四則運算,若除法出入的除數為0,輸出:除數不能為0,其他情況輸出運算及結果。

例如:輸入兩個數為5.2和100,輸出應為:5.2 * 100 = 520 。

//switch-case語句
#include<iostream>

using namespace std;

void main() {
float num1, num2;
int operation;
cout << "please input the operation of num1 and num2(1 - +,2 - -,3 - *,4 - /) :";
cin >> operation;
cout << "please input num1 :";
cin >> num1;
if (operation == 4)
cout << "please input num2 (num2 can't be assigned to zero) :";
else
cout << "please input num2 :";
cin >> num2;

switch (operation)
        {
case 1:
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
break;
case 2:
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
case 3:
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
break;
default:
cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
break;
        }
    }

//if-else語句
#include<iostream>

using namespace std;

void main() {
float num1, num2;
int operation;
cout << "please input the operation of num1 and num2(1 - +,2 - -,3 - *,4 - /) :";
cin >> operation;
cout << "please input num1 :";
cin >> num1;
if (operation == 4)
cout << "please input num2 (num2 can't be assigned to zero) :";
else
cout << "please input num2 :";
cin >> num2;

if (operation == 1)
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
else if (operation == 2)
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
else if (operation == 3)
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
else 
cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;

    }      
【程式設計語言-C++】第九講 C++ 循環語句while、for

3、本節導入

【程式設計語言-C++】第九講 C++ 循環語句while、for

這次要給大家講的是循環語句。循環語句是C++裡非常重要的一部分,我們會經常在以後的代碼中使用到循環語句。

本次課程有錄免費視訊課,講解for循環的應用。講解了非常經典的幾個循環嵌套的例子。視訊課程請點選:

​​https://m.weibo.cn/detail/4360339955606473​​

大家看完這篇部落格,可以點選連結,看一下相關的代碼。

接下來讓我們一起走進循環語句的世界吧

重點知識介紹

【程式設計語言-C++】第九講 C++ 循環語句while、for

1、循環語句

【程式設計語言-C++】第九講 C++ 循環語句while、for

在不少實際問題中有許多具有規律性的重複操作,是以在程式中就需要重複執行某些語句。一組被重複執行的語句稱之為循環體,能否繼續重複,決定循環的終止條件。循環結構是在一定條件下反複執行某段程式的流程結構,被反複執行的程式被稱為循環體。循環語句是由循環體及循環的終止條件兩部分組成的。

C++中循環語言主要有for循環和while循環。

【程式設計語言-C++】第九講 C++ 循環語句while、for

2、for循環

【程式設計語言-C++】第九講 C++ 循環語句while、for

1.基本格式

(1)講解

C++中,for循環的基本格式如下:

for (size_t i = 0; i < length; i++)

    {



    }      

可以把上面的基本格式寫成下面這種樣子:

for (語句1; 條件;語句2)

{

代碼塊。

}

當執行到for循環時,執行過程如下:

1.先執行語句1,判斷是否滿足條件:滿足執行代碼塊,不滿足跳過循環。

2.若上一句滿足條件,執行完代碼塊後,執行語句2,判斷是否滿足條件:滿足執行代碼塊,不滿足跳出循環。

3.若上一句滿足條件不斷執行上一句,直至不滿足為止。

(2)例題

輸出1-20,要求每行輸出5個。

執行代碼結果如下:

#include<iostream>

using namespace std;

void main() {
for (int i = 1; i <= 20; i++)
    {

cout << i  << '\t';
if (i % 5 == 0)
cout << endl;
    }

}      

​執行代碼結果如下:

【程式設計語言-C++】第九講 C++ 循環語句while、for
【程式設計語言-C++】第九講 C++ 循環語句while、for

2.for循環嵌套

【程式設計語言-C++】第九講 C++ 循環語句while、for

(1)講解

for循環最重要的應用就是循環嵌套,循環嵌套可以實作多元數組的指派與周遊,可以輸出乘法口訣與加法口訣等等。

由于這塊内容很重要,我專門錄制了視訊課程,大家請點選下方連結觀看:​​https://m.weibo.cn/detail/4360339955606473​​

(2)例題

  除了這裡的代碼,我給大家再說一個二維數組的指派與周遊操作。

【程式設計語言-C++】第九講 C++ 循環語句while、for

      我們還是要輸出上面這一串數字,隻不過我們要先給二維數組指派,然後再周遊。代碼如下:

#include<iostream>

using namespace std;

void main() {
int arr[4][5];
for (int i = 0; i < 4; i++)
    {
for (int j = 0; j < 5; j++)
        {
            arr[i][j] = 5 * i + j + 1;
        }
    }
for (int i = 0; i < 4; i++)
    {
for (int j = 0; j < 5; j++)
        {
cout << arr[i][j] << '\t';
        }
cout << endl;
    }

}
更多重要的應用,大家請看視訊課程裡面的内容。      

while循環有兩種形式,一種是while語句,一種是do-while語句。

1.while語句

(1)講解

C++中,while語句的基本格式如下:

while (true)

    {

    }      

可以把上面的基本格式寫成下面這種樣子:

while(條件)

 {

               代碼塊。

}

當執行到while語句時,執行過程如下:

1.先判斷是否滿足條件:滿足執行代碼塊,不滿足跳過循環。

2.若上一句滿足條件,執行完代碼塊後,再判斷是否滿足條件:滿足執行代碼塊,不滿足跳出循環。

3.若上一句滿足條件不斷執行上一句,直至不滿足為止。

(2)例題

輸出1-20,要求每行輸出5個。

#include<iostream>

using namespace std;

void main() {
int i = 1;

while (i<=20)
    {
cout << i << '\t';
if (i % 5 == 0)
cout << endl;
        i++;
    }

}      

執行代碼結果如下:

【程式設計語言-C++】第九講 C++ 循環語句while、for

2.do-while語句

(1)講解

C++中,do-while語句的基本格式如下:

do

    {

    } while (true);      

do-while語句和while語句類似,主要有兩點要注意:

1.while語句是先判斷,再執行,而do-while語句是先執行,再做判斷。順序剛剛相反。

2.do-while語句最後要加分号。

(2)例題

這裡我們做一個小功能,結合選擇語句。

利用 switch-case語句判斷小明的成績在班裡的層次(滿分100):

>=90分,優秀;>=80分,良;>=60分,及格;<60,分,不及格。

如果輸入0-100分外的資料,就讓其一直重新輸入。

注:while和do-while語句經常用于不确定循環次數,确定跳出循環條件的循環。

#include<iostream>

using namespace std;

void main() {
float score;
do
    {
cout << "please input socre :";
cin >> score;
    } while (score < 0 || score>100);
switch ((int)score / 10)
    {
case 9:
case 10:
cout << "A" << endl;
break;
case 8:
cout << "B" << endl;
break;
case 7:
cout << "C" << endl;
break;
case 6:
cout << "D" << endl;
break;
default:
cout << "E" << endl;
break;
    }

}      

執行結果如下:

【程式設計語言-C++】第九講 C++ 循環語句while、for

思考題

分别輸出如下圖形:(底和高都為9的直角三角形)

【程式設計語言-C++】第九講 C++ 循環語句while、for
【程式設計語言-C++】第九講 C++ 循環語句while、for
【程式設計語言-C++】第九講 C++ 循環語句while、for

思考題的答案會在下一次的文章中公布哦,希望大家能夠多多練習。

編輯人:吳夢薇

稽核人:李萌 水亦心