一、break
尽管您已在前文 switch 语句(8.5 — switch 语句基础)中见过 break 语句,它仍值得系统阐述,因为 break 亦可用于其他控制流结构。break 语句可立即终止 while、do-while、for 循环或 switch 语句,随后执行流跳转到该循环或 switch 之后的下一条语句。
1.1 在 switch 中使用 break
在 switch 语句中,break 通常置于每个 case 末尾,表示该分支处理完毕,从而防止向下贯穿(fall-through)到后续 case:
#include <iostream>
void printMath(int x, int y, char ch)
{
switch (ch)
{
case '+':
std::cout << x << " + " << y << " = " << x + y << '\n';
break; // 防止向下贯穿
case '-':
std::cout << x << " - " << y << " = " << x - y << '\n';
break;
case '*':
std::cout << x << " * " << y << " = " << x * y << '\n';
break;
case '/':
std::cout << x << " / " << y << " = " << x / y << '\n';
break;
}
}
int main()
{
printMath(2, 3, '+');
return 0;
}
详见 8.6 — switch 贯穿与作用域以获取更多信息。
1.2 在循环中使用 break
在循环中,break 可提前结束整个循环,执行流立即跳到循环后的第一条语句。
示例:
#include <iostream>
int main()
{
int sum{ 0 };
// 允许用户最多输入 10 个数
for (int count{ 0 }; count < 10; ++count)
{
std::cout << "Enter a number to add, or 0 to exit: ";
int num{};
std::cin >> num;
if (num == 0)
break; // 立即退出循环
sum += num;
}
// 退出循环后继续执行此处
std::cout << "The sum of all the numbers you entered is: " << sum << '\n';
return 0;
}
该程序允许用户输入最多 10 个数,输入 0 时提前结束。示例运行:
Enter a number to add, or 0 to exit: 5
Enter a number to add, or 0 to exit: 2
Enter a number to add, or 0 to exit: 1
Enter a number to add, or 0 to exit: 0
The sum of all the numbers you entered is: 8
break 亦常用于退出故意设置的无限循环:
#include <iostream>
int main()
{
while (true) // 无限循环
{
std::cout << "Enter 0 to exit or any other integer to continue: ";
int num{};
std::cin >> num;
if (num == 0)
break;
}
std::cout << "We're out!\n";
return 0;
}
示例运行:
Enter 0 to exit or any other integer to continue: 5
Enter 0 to exit or any other integer to continue: 3
Enter 0 to exit or any other integer to continue: 0
We're out!
1.3 break 与 return 的区别
初学者常混淆 break 与 return。break 终止 switch 或循环,执行流继续到其后的语句;return 终止整个函数,执行流返回到函数调用点。
示例:
#include <iostream>
int breakOrReturn()
{
while (true)
{
std::cout << "Enter 'b' to break or 'r' to return: ";
char ch{};
std::cin >> ch;
if (ch == 'b')
break; // 跳到循环后的第一条语句
if (ch == 'r')
return 1; // 立即返回调用者
}
// 跳出循环后执行此处
std::cout << "We broke out of the loop\n";
return 0;
}
int main()
{
int returnValue{ breakOrReturn() };
std::cout << "Function breakOrReturn returned " << returnValue << '\n';
return 0;
}
两次运行示例:
Enter 'b' to break or 'r' to return: r
Function breakOrReturn returned 1
Enter 'b' to break or 'r' to return: b
We broke out of the loop
Function breakOrReturn returned 0
二、continue
continue 语句提供了一种便捷方式:结束当前迭代,但不终止整个循环,直接进入下一次迭代判断。
示例:
#include <iostream>
int main()
{
for (int count{ 0 }; count < 10; ++count)
{
if ((count % 4) == 0)
continue; // 跳过本次迭代
std::cout << count << '\n';
// continue 跳转到此处
}
return 0;
}
程序输出 0 到 9 中不被 4 整除的数:
1
2
3
5
6
7
9
在 for 循环中,即使遇到 continue,其 end-expression(本例中为 ++count)仍会执行。
需谨慎:在 while 或 do-while 中使用 continue 时,若循环变量在循环体内更新,而 continue 跳过了更新语句,可能导致无限循环。
示例:
#include <iostream>
int main()
{
int count{ 0 };
while (count < 10)
{
if (count == 5)
continue; // 跳至循环底部
std::cout << count << '\n';
++count; // count 为 5 时此语句被跳过
}
return 0;
}
程序意图跳过 5,但输出 0 1 2 3 4 后进入无限循环,因为 count 永远为 5。
三、关于 break 与 continue 的争议
许多教材告诫勿用 break 与 continue,因其改变执行流程,可能降低可读性。例如,复杂逻辑中的 break 易被忽视,或触发条件不明显。
然而,合理运用 break 与 continue 可减少嵌套层次,简化循环逻辑,反而提升可读性。
示例对比:
// 版本 1:使用布尔变量与嵌套
#include <iostream>
int main()
{
int count{ 0 };
bool keepLooping{ true };
while (keepLooping)
{
std::cout << "Enter 'e' to exit or any other character to continue: ";
char ch{};
std::cin >> ch;
if (ch == 'e')
keepLooping = false;
else
{
++count;
std::cout << "We've iterated " << count << " times\n";
}
}
return 0;
}
使用 break 的简化版本:
#include <iostream>
int main()
{
int count{ 0 };
while (true)
{
std::cout << "Enter 'e' to exit or any other character to continue: ";
char ch{};
std::cin >> ch;
if (ch == 'e')
break;
++count;
std::cout << "We've iterated " << count << " times\n";
}
return 0;
}
后者通过单个 break 避免了布尔变量、else 及嵌套块。
continue 在 for 循环顶部使用尤为有效,可避免嵌套。
示例对比:
// 版本 1:嵌套 if
#include <iostream>
int main()
{
for (int count{ 0 }; count < 10; ++count)
{
if ((count % 4) != 0)
{
std::cout << count << '\n';
}
}
return 0;
}
使用 continue 的简化版本:
#include <iostream>
int main()
{
for (int count{ 0 }; count < 10; ++count)
{
if ((count % 4) == 0)
continue;
std::cout << count << '\n';
}
return 0;
}
减少变量与嵌套块,可读性优于 break/continue 带来的跳转成本。
最佳实践:
当 break 与 continue 能够简化循环逻辑时,应当使用。
四、关于提前返回(early return)的类似讨论
与 break 类似,函数中并非位于末尾的 return 被称为提前返回。许多程序员主张避免提前返回,认为单一返回点使函数逻辑简单:参数处理、逻辑执行、结果返回,一目了然;额外返回点增加复杂度。
反方观点认为,提前返回可在任务完成后立即退出,减少不必要的后续阅读,降低嵌套条件,提升可读性。
折中做法:仅在函数顶部进行参数校验(捕获非法参数)时使用提前返回,其余逻辑保持单一返回。
我们认为提前返回利大于弊,但仍需适度艺术化地运用。
最佳实践:
当提前返回能够简化函数逻辑时,应当使用。