How would I accomplish the following?
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
If z=1, both for loops should stop and it should continue on with some other code. This obviously is an oversimplified example of what I'm trying to accomplish. (In other words, I know I need to initialize variables, etc...)
Assuming you don't need the values of y
and x
, just assign them the values that will case both loops to exit:
for (x=0;x<3;x++)
{
for (y=0;y<3;y++)
{
if (z == 1)
{
y = 3 ;
x = 3 ;
}
}
}
Add z
to your outermost conditional expression, and break out of the innermost loop.
for(x = 0; x < 3 && z != 1; x++) {
for(y = 0; y < 3; y++) {
if(z == 1) {
break;
}
}
}
Of course, there's a fair bit of handwaving involved - in the code snippet you've provided, z
isn't being updated. Of course, it'd have to be if this code was to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With