为什么在C语言中在switch语句块中可以使用break,但在if-else语句块中不能呢?

huangapple go评论80阅读模式
英文:

Why does break work in switch blocks but not if-else blocks in C?

问题

刚开始学习C语言时注意到这个细节。不是一个紧急的问题,只是好奇。

我尝试搜索答案,但似乎找不到解释。

(不确定还有什么要说的,这个句子只是为了满足220字符的要求)

英文:

Just beginning with C and noticed this detail. Not a pressing question, just curious.

I tried searching for the answer but couldn't seem to find an explanation.

(not sure what else to say, this sentence is just to satisfy the 220-character requirement)

答案1

得分: 4

一个只退出if语句的break语句将毫无用处。考虑以下代码:

if (test)
{
	A;
	break;
	B;
}

由于break语句退出了if语句,这等效于:

if (test)
{
	A;
}

那么为什么要编写break;以及其后的代码呢?要在这里发挥作用,break必须是有条件的;它必须类似于:

if (test)
{
	A;
    if (要退出)
		break;
	B;
}

但现在break位于另一个if内部,因此它只会退出该if,而不是外部的那个。因此,它无法退出外部的if

此外,如果break退出了一个if语句,而不是包含它的循环或switch语句,那么在循环语句中它将毫无用处。

break语句用于循环(forwhiledo),以在满足某些条件时退出循环。对于这种用法,break语句必须位于一个if内部。例如,在数组中搜索某个元素的循环将具有以下形式:

for (int i = 0; i < N; ++i)
	if (array[i] 符合条件)
		break;

鉴于在for循环中有用的break必须位于if语句内部,终止if语句的break将毫无用处。如果上面的break只退出了if语句,那么它将毫无用处,for循环将继续退出,即使我们希望它停止。

如果确实需要退出if语句,可以使用goto

if (test)
{
	A;
	if (发生错误)
		goto EndOfIf;
	B;
}
EndOfIf:

或者使用包含的循环或switch

switch (0)
{
	default:
	if (test)
	{
		A;
		if (发生错误)
			break;
		B;
	}
}
英文:

A break that only exited an if statement would not be useful. Consider this code:

if (test)
{
	A;
	break;
	B;
}

Since the break exits the if statement, this is equivalent to:

if (test)
{
	A;
}

So why write the break; and the code after it at all? To be useful here, the break would need to be conditional; it would have to be something like:

if (test)
{
	A;
    if (want to exit)
		break;
	B;
}

But now the break is inside another if, so it would only exit that if, not the outer one. So it cannot do the job of exiting the outer if.

Also, if break exited an if statement, rather than a containing loop or switch, then it would not be useful in loop statements.

break is used in loops (for, while, and do) to exit loops when some condition is reached. For this use, the break statement must be inside an if. For example, a loop for searching for some element in an array would have a form like:

for (int i = 0; i &lt; N; ++i)
	if (array[i] matches conditions)
		break;

Given that a useful break in a for loop must be inside an if statement, a break that terminated an if statement would not be useful. If the break above only exited the if statement, it would not be useful, and the for loop would continue exiting even though we want it stopped.

If you do have a need to exit an if statement, you can do it with goto:

if (test)
{
	A;
	if (error occurs)
		goto EndOfIf:
	B;
}
EndOfIf:

or with an enclosing loop or switch:

switch (0)
{
	default:
	if (test)
	{
		A;
		if (error occurs)
			break;
		B;
	}
}

答案2

得分: 0

中断语句在C编程语言中的开关块和if-else块中有不同的工作方式,因为它们被设计用于不同的目的并且结构不同。

在开关块中,中断语句用于提前退出,而if-else块没有这种行为,因为它们没有像开关块那样的“穿透”概念。相反,程序会继续执行if-else链,评估每个条件并执行相应的块,直到一个条件评估为true或没有条件匹配。

switch (value) {
case 1:
    // 如果值为1,执行某些操作
    break; // 处理完case 1后退出开关块
case 2:
    // 如果值为2,执行某些操作
    break;
// ... 其他情况
default:
    // 如果没有任何情况匹配,执行某些操作
}
英文:

The break statement works differently in switch blocks and if-else blocks in the C programming language because they are designed to serve different purposes and are structured differently.

Break statement is used to exit a switch block early, while if-else blocks do not have this behaviour since they don't have the concept of "fall-through" like switch blocks. Instead, the program continues executing through the if-else chain, evaluating each condition and executing the corresponding block until one condition evaluates to true or none of the conditions match.

switch (value) {
case 1:
    // Do something if value is 1
    break; // Exit the switch block after handling case 1
case 2:
    // Do something if value is 2
    break;
// ... other cases
default:
    // Do something if none of the cases match
}

huangapple
  • 本文由 发表于 2023年7月28日 00:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定