4.2.2 do ...while循環(huán)語(yǔ)句
作用:滿足循環(huán)條件,執(zhí)行循環(huán)語(yǔ)句
語(yǔ)法:do{循環(huán)語(yǔ)句}while(循環(huán)條件);
注意:與while的區(qū)別在于do...while會(huì)先執(zhí)行一次循環(huán)語(yǔ)句,再判斷循環(huán)條件
練習(xí)案例:水仙花數(shù)
水仙花數(shù)是指一個(gè)三位數(shù),它的每個(gè)位上的數(shù)字的3次冪之和等于它本身,請(qǐng)利用do...while語(yǔ)句,求出所有3位數(shù)字中的水仙花數(shù)。
#include <iostream>
using namespace std;
#include <cmath>
int main()
{
int num = 100;
int a = 0;
int b = 0;
int c = 0;
int sum = 0;
cout << "水仙花數(shù)有:" << endl;
do
{
a = num % 10;
b = num / 10 % 10;
c = num / 100;
sum = a*a*a + b*b*b + c*c*c;
if (sum == num)
{
cout << num << endl;
}
num++;
} while (num <= 999);
system("pause");
return 0;
}

4.2.3 for循環(huán)語(yǔ)句
作用:滿足循環(huán)條件,執(zhí)行循環(huán)語(yǔ)句
語(yǔ)法:for(起始表達(dá)式;條件表達(dá)式;末尾循環(huán)體){循環(huán)語(yǔ)句;}
練習(xí)案例:敲桌子
案列描述:從1開始數(shù)到數(shù)字100,如果數(shù)字個(gè)位含有7,或者數(shù)字十位含有7,或者該數(shù)字是7的倍數(shù),我們打印敲桌子,其余數(shù)字直接打印輸出。
#include <iostream>
using namespace std;
#include <cmath>
int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
4.2.3 嵌套循環(huán)
作用:在循環(huán)體中再嵌套一層循環(huán),解決一些實(shí)際問題
案例:99乘法口訣表
#include <iostream>
using namespace std;
int main()
{
cout << "9X9乘法口訣表" << endl;
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << " X " << i << " = " << i*j<<"\t" ;
}
cout << endl;
}
cout << endl;
cout << endl;
cout << "99加法表" << endl;
for (int i = 1; i < 10; i++)
{
for (int j = i; j <10; j++)
{
cout << i << " + " << j << " = " << i+j << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
4.3 跳轉(zhuǎn)語(yǔ)句
4.3.1 break語(yǔ)句
作用:用于跳出選擇結(jié)構(gòu)或者循環(huán)結(jié)構(gòu)
break使用時(shí)機(jī):
- 出現(xiàn)switch條件語(yǔ)句中,作用是終止case并跳出switch
- 出現(xiàn)循環(huán)語(yǔ)句中,作用是跳出當(dāng)前的循環(huán)語(yǔ)句
- 出現(xiàn)在嵌套語(yǔ)句中,跳出最近的內(nèi)存循環(huán)語(yǔ)句
4.3.2 continue語(yǔ)句
作用:在循環(huán)語(yǔ)句中,跳過本次循環(huán)中余下尚未執(zhí)行的語(yǔ)句,繼續(xù)執(zhí)行下一次循環(huán)
continue并沒有使整個(gè)循環(huán)終止,而break會(huì)跳出循環(huán)
4.3.3 goto語(yǔ)句
作用:可以無條件跳轉(zhuǎn)語(yǔ)句
語(yǔ)法:goto 標(biāo)記;
解釋:如果標(biāo)記的名稱存在,執(zhí)行到goto語(yǔ)句時(shí),會(huì)跳轉(zhuǎn)到標(biāo)記的位置。
在程序中不建議使用goto語(yǔ)句,以免造成程序流程混亂
5. 數(shù)組
5.1 概述
所謂數(shù)組,就是一個(gè)集合,里面存放了相同類型的數(shù)據(jù)元素
特點(diǎn)1:數(shù)組中的每個(gè)數(shù)據(jù)元素都是相同的數(shù)據(jù)類型
特點(diǎn)2:數(shù)組由連續(xù)的內(nèi)村位置組成的
5.2 一維數(shù)組
5.2.1 一維數(shù)組定義方式
一維數(shù)組的三種定義方式:
數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度];-
數(shù)據(jù)類型 數(shù)組名[數(shù)組長(zhǎng)度] = {值1,值2 ...}如果在初始化數(shù)據(jù)的時(shí)候,沒有全部填寫完,會(huì)用0來填補(bǔ)剩余數(shù)據(jù)
數(shù)據(jù)類型 數(shù)組名[] = {值1,值2 ...}
數(shù)組元素的下標(biāo)是從0開始索引的
5.2.2 一維數(shù)組組名
用途:
可以統(tǒng)計(jì)整個(gè)數(shù)組在內(nèi)存中的長(zhǎng)度
可以獲取數(shù)組在內(nèi)存中的首地址
數(shù)組名是一個(gè)常量,不可以進(jìn)行賦值操作
練習(xí)案例:五只小豬稱體重
案例描述:
在一個(gè)數(shù)組中記錄了五只小豬的體重并打印最重的小豬體重
#include <iostream>
using namespace std;
int main()
{
cout << "請(qǐng)輸入五只小豬體重:" << endl;
int arr[5];
for (int i = 0; i < 5; i++)
{
cout << "第" << i+1 <<"只豬的體重為";
cin >> arr[i];
}
int max = 0;
int j = 0;
int t = 0;
for (j = 0; j < 5; j++)
{
if (arr[j] >= max)
{
max = arr[j];
t = j + 1;
}
}
cout << "最大值體重為"<< max << endl;
cout << "第" << t << "只豬最重" << endl;
system("pause");
return 0;
}
結(jié)果:

案例2:數(shù)組元素逆置
案例描述:請(qǐng)聲明一個(gè)5個(gè)元素的數(shù)組,并且將元素逆置。
#include <iostream>
using namespace std;
int main()
{
//1.創(chuàng)建數(shù)組元素逆置
int arr[5] = {};
cout << "輸入數(shù)組元素" << endl;
for (int k = 0; k < 5; k++)
{
cout << "第" << k << "個(gè)元素為:";
cin >> arr[k];
}
cout << "轉(zhuǎn)換前的順序?yàn)椋?;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//2.實(shí)現(xiàn)逆置
int start = 0;//起始元素下標(biāo)
int end = sizeof(arr) / sizeof(arr[0]) - 1;//結(jié)束下標(biāo)
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
//3.打印逆置后的數(shù)組
cout << "轉(zhuǎn)換后的順序?yàn)椋?;
for (int j = 0; j < 5; j++)
{
cout << arr[j] << " ";
}
cout << endl;
system("pause");
return 0;
}
案列結(jié)果:

5.2.3 冒泡排序
作用:最常用的排序算法,對(duì)數(shù)組內(nèi)元素進(jìn)行排序
- 比較相鄰的元素。如果第一個(gè)比第二個(gè)大,就交換它們。
- 對(duì)每一對(duì)相鄰的元素做同樣的工作,執(zhí)行完畢后,找到一個(gè)最大值
- 重復(fù)以上步驟,每次比較次數(shù)-1,直到不需要比較
5.3 二維數(shù)組
二維數(shù)組就是在一維數(shù)組上,多加一個(gè)維度
5.3.1 二維數(shù)組定義方式
數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {{數(shù)據(jù)1,數(shù)據(jù)2},{數(shù)據(jù)3,數(shù)據(jù)4}};數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};數(shù)據(jù)類型 數(shù)組名[][列數(shù)] = {數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};
利用第二種方式更加直觀,提高代碼可讀性
在定義二維數(shù)組時(shí),如果初始化了數(shù)據(jù),可以省略行數(shù)
5.3.2 二維數(shù)組名
查看二維數(shù)組所占內(nèi)存空間
獲取二維數(shù)組首地址
5.3.3 二維數(shù)組應(yīng)用案例
考試成績(jī)統(tǒng)計(jì):
案例描述:有三名同學(xué)(張三,李四,王五),在一次考試中的成績(jī)分別如下表,請(qǐng)分別輸出三名同學(xué)的總成績(jī)
| 語(yǔ)文 | 數(shù)學(xué) | 英語(yǔ) | |
|---|---|---|---|
| 張三 | 100 | 100 | 100 |
| 李四 | 90 | 50 | 100 |
| 王五 | 60 | 70 | 80 |
#include <iostream>
using namespace std;
#include <string>
int main()
{
string names[3] = { "張三","李四","王五" };
int score[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80}
};
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += score[i][j];
}
cout << names[i] << "的總分:" << sum << endl;
}
system("pause");
return 0;
}
結(jié)果:
代碼地址:https://github.com/smallpotatody/C-_learning/tree/master/day03/code