Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何突破内部for循环并返回父for循环_C++_C - Fatal编程技术网

C++ 如何突破内部for循环并返回父for循环

C++ 如何突破内部for循环并返回父for循环,c++,c,C++,C,我这里有密码 for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { //checking some conditions here for true or false if(false) { break out of this for loop; } else if(true) { pri

我这里有密码

for(int i=0;i<5;i++)
{
    for(int j=0;j<5;j++)
    {
        //checking some conditions here for true or false
        if(false)
        {
            break out of this for loop;
        }
        else if(true)
        {
            printf("true");
        }
    }
}
for(int i=0;i
我试图使用
break
,但控件也移出了父for循环

你一定是糊涂了。
break
只中断最里面的循环/开关,所以它不可能也停止外循环(除非外循环碰巧是在它的最后一次迭代中,这给了你这个错误的印象)

当您对此有疑问时,您可以使用调试器逐步完成代码,或者至少在代码中插入“跟踪”输出,以便验证它的实际功能:

for(int i=0;i<5;i++)
{
    printf("outer loop %d\n", i);
    for(int j=0;j<5;j++)
    {
        printf("inner loop %d\n", j);
        //checking some conditions here for true or false
        if(false)
        {
            printf("breaking out of inner loop\n");
            break;
        }
        else if(true)
        {
            printf("true in inner loop\n");
        }
    }
    printf("finishing the outer loop %d\n", i);
}

for(int i=0;i如果在内环中使用
break
,那么它肯定会将控件移动到外环


我想你可能错过的是在走出内环后重置你的错误状态。

< P>这一切都是基于你想从内部循环中解脱出来的逻辑。考虑下面的步骤。

Do
    For Int x = 1 To 10
        Do
            Break, Break, Break
        Loop
    Next x
Loop
尝试重新考虑您的程序逻辑,并使用标志实现break语句

如果需要,可以通过使用goto语句从内部循环中断来克服

int num[5][5][5];

for (int x = 0; x < 5; x++)
{
    for (int y = 0; y < 5; y++)
    {
        for (int z = 0; z < 5; z++)
        {
            if (num[x][y][z] == 256)
            {
                cout << "Number has been found at location: " << x << y << z;
                //break the three for loops here, it's unnecessary to check any other locations
                goto finish;
            }
        }
    }
}
finish:
int num[5][5][5];
对于(int x=0;x<5;x++)
{
对于(int y=0;y<5;y++)
{
对于(intz=0;z<5;z++)
{
如果(num[x][y][z]==256)
{

coutBreak只会中断使用它的循环
顺便说一句,如果条件为false,它将永远不会执行

是的,您的代码是正确的,它必须按照您的预期工作。 除非您已经编译了此文件并执行了其他文件。:-)

6.8.6.3中断声明

约束条件

1中断语句只能出现在开关体或回路体中或作为开关体或回路体出现

语义学

2 break语句终止最小封闭开关或迭代的执行 声明

引用自ISO/IEC 9899:TC3

因此,您的中断应该可以工作,因为您不使用任何pre-alpha编译器

但问题更多

if (false) //will never be executed, as false is ever fals
{
    //code gets never invoked
}

因此,您不会中断,因为您从未调用
中断;

尝试使用标志并在for循环中检查它

int stop = 0;

for (int i = 0; !stop && i < 5; ++i) {
    for (int j = 0; j < 5; ++j) {
        if (false) {
            stop = 1;
            break;
        }
        // Condition when true does not need 'else' if breaking.
    }
}
int-stop=0;
对于(int i=0;!停止(&i<5;++i){
对于(int j=0;j<5;++j){
if(false){
停止=1;
打破
}
//如果条件为true,则在中断时不需要“else”。
}
}

您确定使用
break
无效吗?
break;
应该有效,
if(false)
将永远不会执行,您的条件不正确?中断应该可以工作。您确定外部循环已中断,还是可能是因为它已运行,并且内部循环的后续调用都已中断,因此未发生任何输出?
中断/继续
仅中断/继续当前循环。任何外部循环都将照常进行,不受影响通过内部循环中的
break/continue
我试图使用break,但控件从父for循环中移出也不可能是真的。可能是外部循环失败的情况,你认为这是由于
break
造成的。这三个中断是怎么回事?@Shahbaz,从do、for和do中断。这不像o如果你问我的话,还有其他例子。代码看起来…很难阅读。OP不想跳出外部循环。事实上,我没有看到伪代码下面的注释。我一直到
跳出这个for循环
,并且做了一个很糟糕的假设。我没有意识到break语句可能这么难使用。对不起,我不应该写我自己的代码f(false)。它在这里造成了混乱。但在我的代码中,它是true。因此我将检查我缺少的内容。@leesei,我如何在if(某些条件)下转到循环的父对象?