《c++入門經典》筆記6
第六章 控制程序流程
6.1 循環
在程序中執行屢次的代碼塊稱爲循環,其中每次循環都稱爲迭代ios
6.2 while循環
while循環致使程序重複執行一組語句,直到開始條件爲false。c++
下面的while循環顯示數字0~99:app
int x = 0;
while(x < 100)
{
std::cout<< x <<"\n";
x++;
}
程序清單6.1中,程序Thirteens使用一個while循環顯示可被13整除且小於500的全部數字。atom
程序清單6.1 Thirteens.cppspa
#include <iostream>
int main()
{
int counter = 0;
while (counter < 500)
{
counter++;
if (counter % 13 == 0)
{
std::cout << counter << " ";
}
}
std::cout << "\n";
return 0;
}
break語句致使循環當即終止,而不等待條件爲false。ip
程序清單6.2這個程序顯示前20個可被14整除的數ci
程序清單6.2 Fourteens.cppstring
#include <iostream>
int main()
{
int counter = 0;
int multiples = 0;
while (true)
{
counter++;
if (counter % 14 == 0)
{
std::cout << counter << " ";
multiples++;
}
if (multiples > 19)
{
break;
}
}
std::cout << "\n";
return 0;
}
另外一種改變循環行爲的方式是使用continue語句。在循環中遇到continue語句時,將跳過餘下的語句,開始下一次循環迭代it
程序清單6.3這個程序顯示前20個可被15整除的數io
程序清單6.3 Fifteens.cpp
#include <iostream>
int main()
{
int counter = 0;
int multiples = 0;
while (multiples < 19)
{
counter++;
if (counter % 15 != 0)
continue;
std::cout << counter << " ";
multiples++;
}
std::cout << "\n";
return 0;
}
6.3 do-wihle循環
while循環執行循環語句前檢查條件表達式,若是條件不可能爲true,循環語句就不會執行。
而do-while語句將在循環末尾檢查條件
也就是do-while循環的循環體至少會執行一次
程序清單6.4 Badger.cpp
#include <iostream>
int main()
{
int badger;
std::cout<<"How many badgers? ";
std::cin>>badger;
do
{
std::cout<<"Badger ";
badger--;
} while (badger > 0);
std::cout<<"\n";
return 0;
}
6.4 for循環
for循環是一種複雜的循環,將設置計數器變量、檢查計數器變量是否知足條件、迭代修改計數器變量這三個步驟合併到了一條語句中。該語句使用關鍵字for,後面是一對括號。括號內,是三條用分號分隔的語句,它們分別初始化計數器、檢查條件和修改計數器
程序清單6.5 MultTable.cpp
#include <iostream>
int main()
{
int number;
std::cout << "Enter a number:";
std::cin >> number;
std::cout << "\nFirst 10 Multiples of " << number << "\n";
for (int counter = 1; counter < 11; counter++)
{
std::cout << number * counter << " ";
}
std::cout << "\n";
return 0;
}
多變量for循環:
for(int x = 0,y = 0; x < 10;x++,y++)
{
std::cout<< x * y << "\n";
}
這個就等同於:
int x = 0,y = 0;
for(; x < 10;x++,y++)
{
std::cout<< x * y << "\n";
}
嵌套循環:
程序清單6.6 BoxMaker.cpp
#include <iostream>
int main()
{
int rows, columns;
char character;
std::cout << "How many rows? ";
std::cin >> rows;
std::cout << "How many columns? ";
std::cin >> columns;
std::cout << "what character to display? ";
std::cin >> character;
std::cout << "\n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
std::cout << character;
}
std::cout << "\n";
}
return 0;
}
6.5 switch語句
switch語句檢查一個表達式,並根據其值執行多個代碼塊中的一個
std::cout<<"You have kulled "<< zombies << " zombie";
switch(zombies)
{
case 0:
std::cout << "s\n";
break;
case 1:
std::cout << "\n";
break;
default:
std::cout << "s\n";
}
在switch語句的case部分,只能進行相等比較,而不能進行關係運算和布爾運算。若是有case值與表達式匹配,將執行相應的語句,而後繼續執行到switch塊末尾或遇到的第一個break語句。若是沒有匹配的case部分,將執行可選的default部分。
在上例中,每一個case部分都以一條break語句結尾,用於退出switch語句。若是case部分末尾沒有break語句,將繼續執行下一個case部分。在有些狀況下,可利用這種辦法來只想多個case部分。
程序清單6.7 BadTeacher.cpp
#include <iostream>
int main()
{
char grade;
std::cout << "Enter your letter grade (ABCDF):";
std::cin >> grade;
switch (grade)
{
case 'A':
std::cout << "Finally!\n";
break;
case 'B':
std::cout << "You can do better!\n";
break;
case 'C':
std::cout << "I'm disappointed in you!\n";
break;
case 'D':
std::cout << "You are not smart!\n";
break;
case 'F':
std::cout << "Get out of my sight!\n";
break;
default:
std::cout << "That's not even a grade!\n";
break;
}
return 0;
}