跳到主要内容

复合条件

到目前为止,我们已经知道如何使用 if 块在 C++ 代码中表示一系列潜在情况。我们还学习了如何通过使用 if/else 结构来编写两种互斥的情况。然后,我们看到了如何通过逻辑运算符将布尔表达式组合在一起。

此外,程序员经常会面临三种或更多互斥的情况。我们可以通过一个新概念有效地处理这种情况:if/else if/else 语句。

复合条件语句

if/else if/else 语句的剖析
if (/* boolean condition */) 
{
// The code in here executes only if the condition evaluates to true
}
else if (/* boolean condition */)
{
// The code in here executes only if the previous condition evaluates to false
// and the this condition evaluates to true
}
else
{
// This code in here executes if and only if all of the previous conditions
// evaluated to false
}

if/else if/else 语句可以表示多个互斥的情况。情况的条件从上到下进行评估,如果其中任何一个为真,它将执行该情况的代码,然后跳出整个结构。如果没有布尔条件评估为真,那么 else 块最终将被执行。因此,整个链中的所有这些情况都是互斥的

else 是完全可选的,并且可以有任意数量的 else if 块。实际上,您可能会意识到前面讨论的 if/else 是没有 else if 的情况。

因此,我们现在可以看到 ifif/elseif/else if/else 实际上都是我们称之为if 语句的一个更大系统。if 语句总是以 if 块开始,可选地后跟任意数量的 else if 块,并可选地后跟 else 块。

只有当条件是互斥的时,才应将它们组合到额外的 else if/else 子句中。如果您有多个独立的条件集,并且希望多个条件可能执行,则应将它们保持为单独的 if 语句。

让我们使用这个新工具进一步升级我们的程序。

重塑先知

上方已经颁布了更多规定。现在,正在实施新的驾照系统。驾驶员将根据其年龄分为不同类别的驾照。规则如下:

  • 年龄在 1821 岁(含)之间的驾驶员获得 C 类驾照
  • 年龄在 2230 岁(含)之间的驾驶员获得 B 类驾照
  • 年龄在 3150 岁(含)之间的驾驶员获得 A 类驾照
  • 年龄在 5164 岁(含)之间的驾驶员获得 C 类驾照
  • 超出这些年龄范围的驾驶员不能获得驾照。
驾照先知 4000
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4000\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
std::cout << "You can legally get a Class C driver's license\n";
} else if (age >= 22 and age <= 30) {
std::cout << "You can legally get a Class B driver's license\n";
} else if (age >= 31 and age <= 50) {
std::cout << "You can legally get a Class A driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
}

分析这个新的 if 语句应该很直接。我们的第一个情况,针对 C 类驾驶员,有两种条件可以颁发:年龄在 18 到 21 岁之间,或者年龄在 51 到 64 岁之间。因此,我们使用复合条件来构建这个逻辑,通过将两个 and 表达式与一个 or 运算符组合。因此,我们同时单独检查两个范围。

当然,您可以在没有 or 运算符的情况下处理 C 类情况,通过将第二个 and 表达式放入专用的 else if 子句中。但是,由于它们最终都执行相同的代码(给予用户 C 类驾照),最好将它们组合成一个条件以避免重复代码。避免重复的一般做法称为不要重复自己,即 DRY,这是一个非常好的理念,需要牢记。遵循 DRY 原则将使代码更具可维护性、可读性,并且减少出现错误的几率。

回到手头的主要话题,其他情况都用 else if 子句清楚地列出。我们使用 and 运算符为每种情况构建一个下限上限。最后,我们使用 else 子句作为“包罗万象”的条件,拒绝所有年龄不属于预定义范围的人。

使用变量减少重复

奇怪的是,尽管我们一直在谈论不要重复自己,但我们几乎重复了相同的代码行三次。请注意,对于 A、B 和 C 类驾照,std::cout << "..." 几乎相同——每种情况只有一个字母不同。我们可以利用这一点,创建一个存储单个字符的变量。然后,我们更改 if 语句以将该变量分配给相应的驾照类别。最后,我们输出与我们创建的驾照变量组合的最终消息。

⚠️ 驾照先知 4500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}

std::cout << "You can legally get a Class " << license_class << " driver's license\n";
}

上面的代码正是我们所描述的。我们创建了一个 char 变量来保存我们的驾照类别,然后我们使用 if 语句将相应的类别分配给该变量。但是,这段代码不会按预期工作!回顾if 语句的剖析,并注意整个 if 语句下方的注释——它说“无论条件是真还是假,这里的代码都会执行。”

所以,如果我们能够合法地获得驾照,代码就能正常工作。如果我们输入 2000 作为我们的出生年份,那么它将按预期打印出 You can legally get a Class C driver's license。但是,如果我们说我们出生在 2015,它将打印出

You cannot legally get a driver's license because you are not between 18 and 64 years old
You can legally get a Class X driver's license

...这是不正确的!它同时打印出两种情况,因为我们把合法输出移到了 if 语句之外。我们需要做的是创建另一个 if 语句,检查我们是否给了用户有效的驾照。我们可以轻松做到这一点,因为我们将 'X' 设置为默认驾照值。这意味着任何未收到驾照的人的 license_class 值仍然是 'X',我们可以通过条件来检查这一点。

驾照先知 4500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} // This else statement was removed to make the print a part of the following if instead

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
}

你可以在这里看到我们如何创建了一个新的 if 语句来检查 license_class != 'X'。这作为选择我们是否给用户驾照的条件语句;然后,我们根据该条件的答案将控制流分派到正确的打印语句。我们现在重新启用了这两个输出的互斥性。

通过使用变量并应用 DRY 原则,您可以看到我们减少了代码的重复。这使得它更具可读性、可维护性,并且更易于调试。

嵌套条件语句

if 语句的内部没有什么特别的。您可以在 if 语句的主体中放入任何有效的可执行代码,就像 main 主体中的所有代码都是有效的可执行代码一样。因此,通过将一个 if 语句放入另一个 if 语句的主体中,应该很容易“嵌套” if 语句。

嵌套 if 语句的示例
if (/* Condition 1 */) {
// Code here gets run if Condition 1 is true

// Since this if statement is inside another if statement,
// it only gets evaluated if the outer if statement's body is entered;
// ie., only if Condition 1 is true
if (/* Condition 2 */) {
// Code here gets run if Condition 2 is true
} else if (/* Condition 3 */) {
// Code here gets run if Condition 2 is false, and Condition 3 is true
}

// Code here gets run regardless of whether Condition 2 or 3 are true,
// however note this is still inside the body of the outer if statement!
} else {
// Code here gets run if Condition 1 is false
}

// Code out here gets run no matter the outcome of the above if statement

在这里,我们可以看到一个嵌套 if 语句在实际中可能是什么样子的存根示例。这里,我们有一个外部 if/else 语句,其中一个 if/else if 语句位于外部语句的主体内部。

当程序运行此代码时,它执行的第一步是检查条件 1 的结果。如果为真,它将进入其主体并执行其中的代码。否则,如果条件 1 为假,它将跳入 else 的主体并执行该代码。整个过程完成后,它将退出整个条件语句并继续执行下面的代码。

注意,在这里,在条件 1 为真的情况下,其主体的一部分是另一个 if 语句。因此,这意味着它将再次做出真/假决定。所以,如果条件 1 为真,它将继续直到它到达嵌套 if 语句。然后,它检查条件 2。如果为真,它执行该主体。否则,如果条件 2 为假,它检查条件 3。如果条件 3 为真,它执行该主体。

请注意这些过程总体上是多么相似。分析外部 if 语句与内部 if 语句的行为时,实际上没有任何改变。if 语句以这种方式非常模块化——它们的行为在任何上下文中都是相同的……在 main 的开头,在 main 的结尾,在另一个 if 语句内部,在两个 if 语句内部,在一个 else 主体内部,等等。

让我们使用这个新工具来进一步升级我们的 Oracle

交通安全监督委员会发布了一项新要求:无论年龄大小,有两次或两次以上事故的人员均无资格获得任何类别驾照。

驾照先知 5000
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5000\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
} else {
std::cout << "You cannot legally get a driver's license because you have " << num_crashes << " accidents\n";
}
}

您可以在这里看到我们将先前代码的一大块移到了 numsCrashes if 语句的主体中。这意味着我们只会在且仅在用户发生少于两次碰撞的情况下执行该代码。否则,如果用户发生两次或更多碰撞,系统将打印出他们无法获得驾照的必要通知。

现在请再次注意我们如何在代码中重复两次相同的消息。这意味着有另一个机会应用 DRY 原则并简化我们的代码以供将来维护。让我们看一下。

再次应用 DRY 原则

驾照先知 5500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases
std::string illegal_reason;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
illegal_reason = "you are not between 18 and 64 years old";
}
} else {
illegal_reason = "you have " + std::to_string(num_crashes) + " accidents";
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because " << illegal_reason << "\n";
}
}

我们可以看到这段代码得到了多方面改进。首先,我们不再重复自己,这意味着如果监督委员会决定增加更多规定,未来的维护将大大简化。

其次,我们现在在“逻辑代码”和“显示代码”之间进行了清晰的划分。如果您将控制台输入/输出命令与逻辑计算命令混杂在一起,代码很快就会变得非常混乱。在 Oracle 5000 程序中,我们确定 license_classillegal_reason 的所有逻辑都在一个 if 语句中,而向用户显示结果的代码在另一个 if 语句中。

复合条件

到目前为止,我们已经知道如何使用 if 块在 C++ 代码中表示一系列潜在情况。我们还学习了如何通过使用 if/else 结构来编写两种互斥的情况。然后,我们看到了如何通过逻辑运算符将布尔表达式组合在一起。

此外,程序员经常会面临三种或更多互斥的情况。我们可以通过一个新概念有效地处理这种情况:if/else if/else 语句。

复合条件语句

if/else if/else 语句的剖析
if (/* boolean condition */) 
{
// The code in here executes only if the condition evaluates to true
}
else if (/* boolean condition */)
{
// The code in here executes only if the previous condition evaluates to false
// and the this condition evaluates to true
}
else
{
// This code in here executes if and only if all of the previous conditions
// evaluated to false
}

if/else if/else 语句可以表示多个互斥的情况。情况的条件从上到下进行评估,如果其中任何一个为真,它将执行该情况的代码,然后跳出整个结构。如果没有布尔条件评估为真,那么 else 块最终将被执行。因此,整个链中的所有这些情况都是互斥的

else 是完全可选的,并且可以有任意数量的 else if 块。实际上,您可能会意识到前面讨论的 if/else 是没有 else if 的情况。

因此,我们现在可以看到 ifif/elseif/else if/else 实际上都是我们称之为if 语句的一个更大系统。if 语句总是以 if 块开始,可选地后跟任意数量的 else if 块,并可选地后跟 else 块。

只有当条件是互斥的时,才应将它们组合到额外的 else if/else 子句中。如果您有多个独立的条件集,并且希望多个条件可能执行,则应将它们保持为单独的 if 语句。

让我们使用这个新工具进一步升级我们的程序。

重塑先知

上方已经颁布了更多规定。现在,正在实施新的驾照系统。驾驶员将根据其年龄分为不同类别的驾照。规则如下:

  • 年龄在 1821 岁(含)之间的驾驶员获得 C 类驾照
  • 年龄在 2230 岁(含)之间的驾驶员获得 B 类驾照
  • 年龄在 3150 岁(含)之间的驾驶员获得 A 类驾照
  • 年龄在 5164 岁(含)之间的驾驶员获得 C 类驾照
  • 超出这些年龄范围的驾驶员不能获得驾照。
驾照先知 4000
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4000\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
std::cout << "You can legally get a Class C driver's license\n";
} else if (age >= 22 and age <= 30) {
std::cout << "You can legally get a Class B driver's license\n";
} else if (age >= 31 and age <= 50) {
std::cout << "You can legally get a Class A driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
}

分析这个新的 if 语句应该很直接。我们的第一个情况,针对 C 类驾驶员,有两种条件可以颁发:年龄在 18 到 21 岁之间,或者年龄在 51 到 64 岁之间。因此,我们使用复合条件来构建这个逻辑,通过将两个 and 表达式与一个 or 运算符组合。因此,我们同时单独检查两个范围。

当然,您可以在没有 or 运算符的情况下处理 C 类情况,通过将第二个 and 表达式放入专用的 else if 子句中。但是,由于它们最终都执行相同的代码(给予用户 C 类驾照),最好将它们组合成一个条件以避免重复代码。避免重复的一般做法称为不要重复自己,即 DRY,这是一个非常好的理念,需要牢记。遵循 DRY 原则将使代码更具可维护性、可读性,并且减少出现错误的几率。

回到手头的主要话题,其他情况都用 else if 子句清楚地列出。我们使用 and 运算符为每种情况构建一个下限上限。最后,我们使用 else 子句作为“包罗万象”的条件,拒绝所有年龄不属于预定义范围的人。

使用变量减少重复

奇怪的是,尽管我们一直在谈论不要重复自己,但我们几乎重复了相同的代码行三次。请注意,对于 A、B 和 C 类驾照,std::cout << "..." 几乎相同——每种情况只有一个字母不同。我们可以利用这一点,创建一个存储单个字符的变量。然后,我们更改 if 语句以将该变量分配给相应的驾照类别。最后,我们输出与我们创建的驾照变量组合的最终消息。

⚠️ 驾照先知 4500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}

std::cout << "You can legally get a Class " << license_class << " driver's license\n";
}

上面的代码正是我们所描述的。我们创建了一个 char 变量来保存我们的驾照类别,然后我们使用 if 语句将相应的类别分配给该变量。但是,这段代码不会按预期工作!回顾if 语句的剖析,并注意整个 if 语句下方的注释——它说“无论条件是真还是假,这里的代码都会执行。”

所以,如果我们能够合法地获得驾照,代码就能正常工作。如果我们输入 2000 作为我们的出生年份,那么它将按预期打印出 You can legally get a Class C driver's license。但是,如果我们说我们出生在 2015,它将打印出

You cannot legally get a driver's license because you are not between 18 and 64 years old
You can legally get a Class X driver's license

...这是不正确的!它同时打印出两种情况,因为我们把合法输出移到了 if 语句之外。我们需要做的是创建另一个 if 语句,检查我们是否给了用户有效的驾照。我们可以轻松做到这一点,因为我们将 'X' 设置为默认驾照值。这意味着任何未收到驾照的人的 license_class 值仍然是 'X',我们可以通过条件来检查这一点。

驾照先知 4500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 4500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} // This else statement was removed to make the print a part of the following if instead

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
}

你可以在这里看到我们如何创建了一个新的 if 语句来检查 license_class != 'X'。这作为选择我们是否给用户驾照的条件语句;然后,我们根据该条件的答案将控制流分派到正确的打印语句。我们现在重新启用了这两个输出的互斥性。

通过使用变量并应用 DRY 原则,您可以看到我们减少了代码的重复。这使得它更具可读性、可维护性,并且更易于调试。

嵌套条件语句

if 语句的内部没有什么特别的。您可以在 if 语句的主体中放入任何有效的可执行代码,就像 main 主体中的所有代码都是有效的可执行代码一样。因此,通过将一个 if 语句放入另一个 if 语句的主体中,应该很容易“嵌套” if 语句。

嵌套 if 语句的示例
if (/* Condition 1 */) {
// Code here gets run if Condition 1 is true

// Since this if statement is inside another if statement,
// it only gets evaluated if the outer if statement's body is entered;
// ie., only if Condition 1 is true
if (/* Condition 2 */) {
// Code here gets run if Condition 2 is true
} else if (/* Condition 3 */) {
// Code here gets run if Condition 2 is false, and Condition 3 is true
}

// Code here gets run regardless of whether Condition 2 or 3 are true,
// however note this is still inside the body of the outer if statement!
} else {
// Code here gets run if Condition 1 is false
}

// Code out here gets run no matter the outcome of the above if statement

在这里,我们可以看到一个嵌套 if 语句在实际中可能是什么样子的存根示例。这里,我们有一个外部 if/else 语句,其中一个 if/else if 语句位于外部语句的主体内部。

当程序运行此代码时,它执行的第一步是检查条件 1 的结果。如果为真,它将进入其主体并执行其中的代码。否则,如果条件 1 为假,它将跳入 else 的主体并执行该代码。整个过程完成后,它将退出整个条件语句并继续执行下面的代码。

注意,在这里,在条件 1 为真的情况下,其主体的一部分是另一个 if 语句。因此,这意味着它将再次做出真/假决定。所以,如果条件 1 为真,它将继续直到它到达嵌套 if 语句。然后,它检查条件 2。如果为真,它执行该主体。否则,如果条件 2 为假,它检查条件 3。如果条件 3 为真,它执行该主体。

请注意这些过程总体上是多么相似。分析外部 if 语句与内部 if 语句的行为时,实际上没有任何改变。if 语句以这种方式非常模块化——它们的行为在任何上下文中都是相同的……在 main 的开头,在 main 的结尾,在另一个 if 语句内部,在两个 if 语句内部,在一个 else 主体内部,等等。

让我们使用这个新工具来进一步升级我们的 Oracle

交通安全监督委员会发布了一项新要求:无论年龄大小,有两次或两次以上事故的人员均无资格获得任何类别驾照。

驾照先知 5000
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5000\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because you are not between 18 and 64 years old\n";
}
} else {
std::cout << "You cannot legally get a driver's license because you have " << num_crashes << " accidents\n";
}
}

您可以在这里看到我们将先前代码的一大块移到了 numsCrashes if 语句的主体中。这意味着我们只会在且仅在用户发生少于两次碰撞的情况下执行该代码。否则,如果用户发生两次或更多碰撞,系统将打印出他们无法获得驾照的必要通知。

现在请再次注意我们如何在代码中重复两次相同的消息。这意味着有另一个机会应用 DRY 原则并简化我们的代码以供将来维护。让我们看一下。

再次应用 DRY 原则

驾照先知 5500
#include <iostream>

int main() {
std::cout << "Welcome to the Driver's License Oracle 5500\n";

int year_of_birth;
std::cout << "Please enter your year of birth: ";
std::cin >> year_of_birth;

int num_crashes;
std::cout << "Please enter the number of crashes you have had: \n";
std::cin >> num_crashes;

char license_class = 'X'; // X is chosen as a default value to signal if we miss one of our cases
std::string illegal_reason;

if (num_crashes < 2) {
// Year 2022 at the moment of writing this lesson
int age = 2022 - year_of_birth;

if ((age >= 18 and age <= 21) or (age >= 51 and age <= 64)) {
license_class = 'C';
} else if (age >= 22 and age <= 30) {
license_class = 'B';
} else if (age >= 31 and age <= 50) {
license_class = 'A';
} else {
illegal_reason = "you are not between 18 and 64 years old";
}
} else {
illegal_reason = "you have " + std::to_string(num_crashes) + " accidents";
}

if (license_class != 'X') {
std::cout << "You can legally get a Class " << license_class << " driver's license\n";
} else {
std::cout << "You cannot legally get a driver's license because " << illegal_reason << "\n";
}
}

我们可以看到这段代码得到了多方面改进。首先,我们不再重复自己,这意味着如果监督委员会决定增加更多规定,未来的维护将大大简化。

其次,我们现在在“逻辑代码”和“显示代码”之间进行了清晰的划分。如果您将控制台输入/输出命令与逻辑计算命令混杂在一起,代码很快就会变得非常混乱。在 Oracle 5000 程序中,我们确定 license_classillegal_reason 的所有逻辑都在一个 if 语句中,而向用户显示结果的代码在另一个 if 语句中。