如何打破内部for循环并返回parent for循环

For*_*Bat 2 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)
        {
            printf("true");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想打破内部for循环并继续外循环.我尝试使用,break但控件也移出了父for循环.

对此有何解决方案?

sya*_*yam 7

我尝试使用,break但控件也移出了父for循环.

你一定很困惑.break只打破了最里面的循环/开关,所以它也不能停止外循环(除非偶然外循环在最后一次迭代,这给你这个错误的印象).

当您对此有疑问时,您可以使用调试器单步调试代码,或者至少在代码中插入"tracing"输出,以便您可以验证它实际执行的操作:

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);
}
Run Code Online (Sandbox Code Playgroud)