"打破;"出"若"声明? [英] "break;" out of "if" statement?
问题描述
你能if语句打出来的还是它会造成死机?我开始用C来认识自己,但这似乎争议。第一个图像是从一本书基于C
(深入浅出C)和片段展示了由哈佛大学的CS类的工作人员书面code。什么是真正回事,有它是与C类标准?
符不if语句打破。
在1990年1月15日,AT& T公司的长途电话系统崩溃,以及60,000人失去了他们的电话服务。原因在哪里?开发人员在交流中使用的C code工作试图用
破
打出来的如果
语句。但是,如果取值破
■不要打破的。相反,程序跳过code的整个部分,并介绍了在9小时打断70000000电话中的错误。
块引用>的(大小= 0;大小< HAY_MAX;大小++)
{
//等待,直到干草EOF
的printf(\\ nhaystack内容[%d] =,大小);
INT稻草=调用getInt();
如果(秸秆== INT_MAX)
打破; //添加干草堆叠
草垛[大小] =稻草;
}
的printf(\\ n);
解决方案
破
与最接近的封闭循环或开关完全交互,不管它是为
,,而
或DO ..而
键入。它经常被称为变相的跳转,在C所有循环其实可以转化成一组条件goto方法的:为(A,B; C)D;
//转化为
一个;
转到测试;
环:D;
ITER:C;
测试:如果(B)转到循环;
结束:而(B)D; //根本没有A或C
做{D; }而(B); //省略初始转到测试
继续; //转到ITER;
打破; //转到结束;不同的是,
继续
和破
虚拟标签由编译器自动放置交互。这是类似于收益
确实因为你知道它会永远在程序流程跳跃前进。交换机是稍微复杂,生成标签和计算转移的阵列,但断裂的工作方式与他们相类似。编程错误通知是指被误解
破
与封闭块,而不是一个封闭的循环互动。试想一下:为(A; B; C){
D组;
如果(E){
F;
如果(G)破; //错误假设,如果突破(E),打破了()
H;
}
一世;
}
焦耳;有人认为,鉴于这样一块code,即
将导致一个跳转到 I
,但它跳转到Ĵ
。预定的功能将使用若H(G!);
来代替。Can you break out of an if statement or is it going to cause crashes? I'm starting to acquaint myself with C, but this seems controversial. The first image is from a book on C ("Head First C") and the snippet shows code written by Harvard's CS classes staff. What is actually going on and has it something to do with C standards?
breaks don't break if statements.
On January 15, 1990, AT&T's long-distance telephone system crashed, and 60,000 people lost their phone service. The cause? A developer working on the C code used in the exchanges tried to use a
break
to break out of anif
statement. Butbreak
s don't break out ofif
s. Instead, the program skipped an entire section of code and introduced a bug that interrupted 70 million phone calls over nine hours.
for (size = 0; size < HAY_MAX; size++) { // wait for hay until EOF printf("\nhaystack[%d] = ", size); int straw = GetInt(); if (straw == INT_MAX) break; // add hay to stack haystack[size] = straw; } printf("\n");
解决方案
break
interacts solely with the closest enclosing loop or switch, whether it be afor
,while
ordo .. while
type. It is frequently referred to as a goto in disguise, as all loops in C can in fact be transformed into a set of conditional gotos:for (A; B; C) D; // translates to A; goto test; loop: D; iter: C; test: if (B) goto loop; end: while (B) D; // Simply doesn't have A or C do { D; } while (B); // Omits initial goto test continue; // goto iter; break; // goto end;
The difference is,
continue
andbreak
interact with virtual labels automatically placed by the compiler. This is similar to whatreturn
does as you know it will always jump ahead in the program flow. Switches are slightly more complicated, generating arrays of labels and computed gotos, but the way break works with them is similar.The programming error the notice refers to is misunderstanding
break
as interacting with an enclosing block rather than an enclosing loop. Consider:for (A; B; C) { D; if (E) { F; if (G) break; // Incorrectly assumed to break if(E), breaks for() H; } I; } J;
Someone thought, given such a piece of code, that
G
would cause a jump toI
, but it jumps toJ
. The intended function would useif (!G) H;
instead.这篇关于&QUOT;打破;&QUOT;出&QUOT;若&QUOT;声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!