c++基礎(三)
選擇結構
if語句:
if語句的語法形式
if (表達式) 語句
例:if (x > y) cout << x;
if (表達式) 語句1 else 語句2
例:if (x > y) cout << x;
else cout << y;
if (表達式1) 語句1
else if (表達式2) 語句2
else if (表達式3) 語句3
…
else 語句 n
例2-2輸入一個年份,判斷是否閏年
#include <iostream> using namespace std;
int main() {int year; bool isLeapYear; cout << "Enter the year: "; cin >> year; isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)); if (isLeapYear) cout << year << " is a leap year" << endl;else cout << year << " is not a leap year" << endl; return 0; }
嵌套的if結構
語法形式:
if( )
if( ) 語句 1
else 語句 2
else
if( ) 語句 3
else 語句 4
註意:語句 1、2、3、4 可以是復合語句;每層的 if 與 else 配對,或用 { } 來確定層次關系。
例2-3:輸入兩個整數,比較兩個數的大小
#include<iostream> using namespacestd; int main() { int x, y; cout << "Enter x and y:"; cin >> x >> y; if (x != y) { if (x > y) cout << "x > y" << endl; else } cout << "x < y" << endl; else{ cout << "x = y" << endl; } return 0; }
switch語句
語法形式
switch (表達式)
{ case 常量表達式 1:語句1
case 常量表達式 2:語句2
case 常量表達式 n:語句n
default : 語句n+1
}
執行順序: 以case中的常量表達式值為入口標號,由此開始順序執行。因此,每個case分支最後應該加break語句。
註意: case分支可包含多個語句,且不用{ }。
表達式、判斷值都是int型或char型。
如果若幹分支執行內容相同可共用一組語句。
例2-4:輸入一個0~6的整數,轉換成星期輸出
#include<iostream> #include<bitset> using namespace std; int main() { int day; cin >> day; switch (day) { case 0: cout << "Sunday" << endl; break; case 1: cout << "Monday" << endl; break; case 2: cout << "Tuesday" << endl; break; case 3: cout << "Wednesday" << endl; break; case 4: cout << "Thursday" << endl; break; case 5: cout << "Friday" << endl; break; case 6: cout << "Saturday" << endl; break; default: cout << "Day out of range Sunday .. Saturday" << endl; break; } return 0; }
例2-4:編寫一個共用執行語句的分支
#include<iostream> using namespace std; int main() { int i; cin >> i; switch (i) { case 0: case 1: case 2: cout << "Hello world!" << endl; break; default: cout << "Hello c++!!!" << endl; break; } //當i=0,1,2時,都有相同的輸出。 return 0; }
while語句
語法形式:while (表達式) 語句
執行順序:先判斷表達式的值,若為 true 時,執行語句。
例2-5 求自然數1~10之和
#include <iostream> using namespace std; int main() { int i = 1, sum = 0; while (i <= 10) { sum += i; //相當於sum = sum + i; i++; } cout << "sum = " << sum << endl; return 0; }
do-while語句
do-while 語句的語法形式
do 語句 // 可以是復合語句,其中必須含有改變條件表達式值的語句。
while (表達式)
執行順序:先執行循環體語句,後判斷條件。表達式為 true 時,繼續執行循環體。
例2-6:輸入一個數,將各位數字翻轉後輸出
#include <iostream> using namespace std; int main() { int n, right_digit, newnum = 0; cout << "Enter the number: "; cin >> n; cout << "The number in reverse order is "; do { right_digit = n % 10; cout << right_digit; n /= 10; /*相當於n=n/10*/ } while (n != 0); cout << endl; return 0; }
例2-7用do-while語句編程,求自然數1~10之和
#include <iostream> using namespace std; int main() { int i = 1, sum = 0; do { sum += i; i++; } while (i <= 10); cout << "sum = " << sum << endl; return 0; }
對比下面的程序
程序1
#include <iostream> using namespace std; int main() { int i, sum = 0; cin >> i; while (i <= 10) { sum += i; i++; } cout << "sum= " << sum << endl; return 0; }
程序2:
#include<iostream> using namespace std; int main() { int i, sum = 0; cin >> i; do { sum += i; i++; } while (i <= 10); { cout << "sum=" << sum << endl; } return 0; }
for語句
for語句語法形式:
for語句的另一種形式:範圍for語句:
for (聲明:表達式)
語句
自定義類型
類型別名:為已有類型另外命名
typedef 已有類型名 新類型名表
例:
typedef double Area, Volume;
typedef int Natural;
Natural i1,i2;
Area a;
Volume v;
using 新類型名 = 已有類型名;
例:using Area = double;using Volume = double;
枚舉類型
定義方式:將全部可取值一一列舉出來。
語法形式:enum 枚舉類型名 {變量值列表};
例:enum Weekday {SUN, MON, TUE, WED, THU, FRI, SAT};
默認情況下SUN=0,MON=1,TUE=2,......,SAT=6
C++包含兩種枚舉類型:
不限定作用域枚舉類型:enum 枚舉類型名 {變量值列表};
限定作用域的enum類將在第4章介紹。
不限定作用域枚舉類型說明:
枚舉元素是常量,不能對它們賦值
例如有如下定義:enum Weekday {SUN, MON, TUE, WED, THU, FRI, SAT};不能寫賦值表達式:SUN = 0,枚舉元素具有默認值,它們依次為: 0,1,2,......。
也可以在聲明時另行指定枚舉元素的值,如:enum Weekday{SUN=7,MON=1,TUE,WED, THU,FRI,SAT};枚舉值可以進行關系運算。
整數值不能直接賦給枚舉變量,如需要將整數賦值給枚舉變量,應進行強制類型轉換。
枚舉值可以賦給整型變量。
例2-11
設某次體育比賽的結果有四種可能:勝(WIN)、負(LOSE)、平局(TIE)、比賽取消(CANCEL),編寫程序順序輸出這四種情況。
分析:
比賽結果只有四種可能,可以聲明一個枚舉類型。
1 #include <iostream> 2 using namespace std; 3 4 enum GameResult { WIN, LOSE, TIE, CANCEL }; 5 int main() { 6 GameResult result; 7 enum GameResult omit = CANCEL; 8 for (int count = WIN; count <= CANCEL; count++) 9 { 10 result = GameResult(count); 11 if (result == omit) 12 { 13 cout << "The game was cancelled" << endl; 14 } 15 else 16 { 17 cout << "The game was played "; 18 if (result == WIN) 19 { 20 cout << "and we won!"; 21 } 22 if (result == LOSE) 23 { 24 cout << "and we lost."; 25 } 26 cout << endl; 27 } 28 } 29 return 0; 30 }
auto類型與decltype類型
auto:編譯器通過初始值自動推斷變量的類型
例如:auto val = val1 + val2;
如果val1+val2是int類型,則val是int類型;
如果val1+val2是double類型,則val是double類型。
decltype:定義一個變量與某一表達式的類型相同,但並不用該表達式初始化變量
例如:decltype(i) j = 2;
c++基礎(三)