-2

It is possible to use break/continue in 2 loops without variable ? (no continue 2; or break 2;)

Example doesn't work :

while(1) {
  // some code
  while(2) {
    // some code
    if(expr) {
      break; // break while(2)
      continue; // continue while(1) but never used
    }
    // some code
  }
  // some code
}

Solution with variable :

while(1) {
  // some code
  $continue = false;
  while(2) {
    // some code
    if(expr) {
      $continue = true;
      break;
    }
    // some code
  }
  if($continue) {
    continue;
  }
  // some code
}

Any solution with break / continue in the while(2) loop ? Another best way ?

Edit. Exemple with datas :

for($i=0; $i < 100; $i++) {
    $a = mt_rand(0, 1000);
    for($j=0; $j < 100; $j++) {
      if($j === $a) {
        break; // and continue the first loop
      }
    }
    echo "how to never display this string if second loop break?";
} 
Jeremy
  • 279
  • 2
  • 13
  • 1
    If I'm understanding you correctly you shouldn't need a `continue` in your `while(1)` as once `while(2)` breaks, the loop would start again and subsequently restart `while(2)` until the condition is met again. The Cycle wouldn't end until you `break;` `while(1)` – scottevans93 Apr 12 '16 at 12:54
  • I guess, just in case it needs to skip something inside, using `if(expr) continue;` will do – Chay22 Apr 12 '16 at 12:59
  • 1
    This code will be horrible to maintain and a total nightmare to every programmer. – ST2OD Apr 12 '16 at 13:04
  • lol i known, i don't make this code but i'm curious, juste a technical question. I write another code. – Jeremy Apr 12 '16 at 13:07

1 Answers1

0

You don't need the variables, just leave the continue out of the "while(2)" if "expr" is true it will break "while(2)" and continues with "while(1)".

GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40