C++ 中的 if 和 if-else 語句用於根據條件控制程序的執行流程,配合邏輯運算符可以構建複雜的判斷邏輯。
一、if / if-else 語句
1. 基本 if 語句
if (條件表達式) {
// 條件為真(true)時執行的代碼
}
如果條件表達式的值為 非零(即邏輯上為 true),則執行大括號內的語句。
2. if-else 語句
if (條件表達式) {
// 條件為真時執行
} else {
// 條件為假(false)時執行
}
3. if-else if-else 多分支結構
if (條件1) {
// 條件1為真時執行
} else if (條件2) {
// 條件1為假且條件2為真時執行
} else {
// 所有條件都為假時執行
}
4.示例
#include<iostream>
using namespace std;
int main() {
// 統計一個句子或者文章有多少個單詞,多少內容
string text = "my name is xaye";
int spaces = 0;
int total = 0;
for (int i = 0; i < text.length(); ++i) {
char current = text[i];
if (current == ' ') { // 當前 == ' '
spaces++;
}
total++;
}
cout << "total = " << total << ", spaces = " << spaces << endl;
return 0;
}
xaye@orange:~/code/dev/19$ ./a.out
total = 15, spaces = 3
if...else...
#include<iostream>
using namespace std;
int main() {
//想讓用户輸入一個分數,我們輸出及格和不及格
// int number;
// cout << "請輸入一個分數:";
// cin >> number;
// 示例1
// if (number >= 60) {
// cout << "及格" << endl;
// } else {
// cout << "不及格" << endl;
// }
// 示例2
// if (number < 30) {
// cout << "D" << endl;
// } else if (number < 60){
// cout << "C" << endl;
// } else if (number < 90) {
// cout << "B" << endl;
// } else {
// cout << "A" << endl;
// }
// 示例3 (猜數字)
int favorite_number = 89;
cout << "Enter a number in the rang 1-100 to find my favorite number:" << endl;
int number = 0;
do {
cin >> number;
if (number > favorite_number) {
cout << "Too high -- guess again:" << endl;
} else if (number < favorite_number) {
cout << "Too low -- guess again:" << endl;
} else {
cout << "is right" << endl;
}
} while (number != favorite_number);
return 0;
}
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個分數:80
及格
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個分數:65
B
xaye@orange:~/code/dev/19$ ./a.out
Enter a number in the rang 1-100 to find my favorite number:
50
Too low -- guess again:
90
Too high -- guess again:
75
Too low -- guess again:
85
Too low -- guess again:
89
is right
二、邏輯運算符
1.示例
或
/*
3.邏輯運算符:|| && !
|| -> or 或者
&& -> and 並且
! -> not 非
*/
#include<iostream>
using namespace std;
int main(){
// 輸入 y yes Y 通過
// n no N 不通過
cout << "This program may reformat your hard disk \n and destory all your data.\n Do you wish to continue? <y/n>";
string user_input;
cin >> user_input;
if (user_input == "y" || user_input == "yes" || user_input == "Y") {
cout << "You were warned!" << endl;
} else if ((user_input == "n" || user_input == "no" || user_input == "N")) {
cout << "A wise choice ... bye" << endl;
} else {
cout << "輸入有誤" << endl;
}
return 0;
}
xaye@orange:~/code/dev/19$ ./a.out
This program may reformat your hard disk
and destory all your data.
Do you wish to continue? <y/n>y
You were warned!
xaye@orange:~/code/dev/19$ ./a.out
This program may reformat your hard disk
and destory all your data.
Do you wish to continue? <y/n>n
A wise choice ... bye
xaye@orange:~/code/dev/19$ ./a.out
This program may reformat your hard disk
and destory all your data.
Do you wish to continue? <y/n>d
輸入有誤
and
/*
3.邏輯運算符:|| && !
|| -> or 或者
&& -> and 並且
! -> not 非
*/
#include<iostream>
using namespace std;
int main(){
int number;
cout << "請輸入一個分數[0 - 100]:";
cin >> number;
if (number >= 0 && number < 30) {
cout << "D" << endl;
} else if (number >= 30 && number < 60){
cout << "C" << endl;
} else if (number >= 60 && number < 90) {
cout << "B" << endl;
} else if (number >= 90 && number < 100) {
cout << "A" << endl;
} else {
cout << "錯誤數字" << endl;
}
return 0;
}
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個分數[0 - 100]:99
A
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個分數[0 - 100]:66
B
非
/*
3.邏輯運算符:|| && !
|| -> or 或者
&& -> and 並且
! -> not 非
*/
#include<iostream>
using namespace std;
#include <climits>
bool is_int_range(long number) {
return number <= INT_MAX && number > INT_MIN;
}
int main(){
cout << "請輸入一個數字:";
long number = 0;
cin >> number;
if (!is_int_range(number)) { // 是不是超出 int 範圍
cout << "輸入的數字超出了int類型的範圍" << endl;
}
return 0;
}
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個數字:9999999999
輸入的數字超出了int類型的範圍
xaye@orange:~/code/dev/19$ ./a.out
請輸入一個數字:22222222