如果value为true,则退出两个for循环

cod*_*ude 4 c loops

我将如何完成以下任务?

for (x=0;x<3;x++) {
    for (y=0;y<3;y++) {
        if (z == 1) {
            // jump out of the two for loops
        }
     }
}
// go on to do other things
Run Code Online (Sandbox Code Playgroud)

如果z = 1,则两个for循环都应该停止,并且应该继续使用其他一些代码.这显然是我想要完成的一个过于简单的例子.(换句话说,我知道我需要初始化变量等...)

thi*_*his 5

假设你并不需要的价值yx,只是为它们分配,将区分两个值循环退出:

for (x=0;x<3;x++) 
{
    for (y=0;y<3;y++) 
    {
        if (z == 1) 
        {
           y = 3 ;
           x = 3 ;
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 假设实际代码在内部循环后没有做任何有趣的事情 (3认同)