打破一个for循环的出 [英] Breaking out of a for-loop
问题描述
我应该使用code
for(i=1;i<4;i++)
{
for(j=1;j,4;j++)
{
printf("Running i=%d j=%d\n", i, j);
}
}
...这个code,打破它的循环
... with this code to break it out of its loop
if(i==2&&j ==1){
printf("Break inner loop when i=%d and j=%d\n", i, j);
break;
}
我的教科书说,在内环块的一开始就插入这个破
语句。我不知道哪里是!我试过很多地方已经,而且仍然无法搞清楚。
在这里我的整个程序:
My textbook said to insert this break
statement at the very beginning of the inner loop block. I don't know where that is! I've tried a lot of places already, and still can't figure it out.
here my whole program:
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j,4;j++)
if(i==2&&j ==1){
printf("Break inner loop when i=%d and j=%d\n", i, j);
break;
}
printf("Running i=%d j=%d\n", i, j);
}
}
return 0;
}
想通了:它有一个错字
Figured it out: it had a typo
推荐答案
内环块的开始是刚过 {
第二为
:
The beginning of the inner-loop block is just after the {
on the second for
:
for(i=1;i<4;i++)
{
for(j=1;j,4;j++)
{
// <<<--- They mean here.
printf("Running i=%d j=%d\n", i, j);
}
}
请注意,破
只会跳出内部循环。外环将再次绕过去。如果你需要摆脱的两个的循环,你将需要添加某种标志。
Note that the break
will only break out of the inner loop. The outer loop will go around again. If you need to break out of both loops you will need to add a flag of some sort.
只注意到你在你的内部循环一个错字,实际上是有效的C(逗号操作符)。它应该是&LT;
:
Just noticed that you have a typo in your inner loop that is actually valid C (the comma operator). It should have been <
:
for(j=1;j<4;j++)
这篇关于打破一个for循环的出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!