var h =100; var t =100; while ((h<=0) || (t<=0)) { ... цикл операции на уменьшение значение h или t... } else if (h<=0) {alert('h параметр = 0'); } else if (t<=0) alert('t параметр = 0'); 

for some reason the logical operator || or it does not work. only when both conditions (h<=0) (t<=0) are equal to 0 exits the loop. I would like an example of how to exit the loop when one of the variables <= 0 Thanks in advance.

  • the code is big too. Subtracting from variables, not uniform. and when one of them will be <= 0 There must be a way out. - Freekazoid
  • the output is triggered when one of the variables goes to - and the second = 0. - Freekazoid
  • syntax error in the above code: you cannot write while (...) { } else . discard all unnecessary from your code and provide a minimal reproducible example - Grundy
  • You wrote только когда оба условия (h<=0) (t<=0) равны 0 выходит из цикла ..... and now одна из переменных уходит в - а вторая = 0 .... we should decide) and it is better to show the code more than it is ..... otherwise this is all the fortune telling on tarot cards - Alexey Shimansky
  • Em. Everything is there. I would like an example of how this can be implemented. 'else if' is not required, it is done from ignorance. - Freekazoid

2 answers 2

You can use break to interrupt the loop.

Check the desired condition inside the loop and call break;

 var i = 0; var j = 10; while (i <= 10 || j <= 10) { document.write('<div>i=', i, '</div>'); i += 1; if (i == 5) break; } document.write('<div>Complete</div>'); 

  • I need a cycle with a double condition. - Freekazoid
  • @ Danil, where did you get it from? :-) You did not provide the code. Apparently he already works as it should. and as a last resort you can use the method from the answer. - Grundy
  • @ Danil, here’s a double example - no difference - Grundy

I would like an example of how to exit the loop when one of the variables <= 0

 while ((h>0) && (t>0)) { ... цикл операции на уменьшение значение h или t... } if (h<=0) alert('h параметр <= 0'); if (t<=0) alert('t параметр <= 0'); 
  • && is a logical operator and I need or. It is necessary that it would work if one of the catches is true. - Freekazoid
  • @Danil, the cycle will be executed while h and p greater than 0. As soon as one of them becomes less than or equal to 0, the cycle is completed. Do not you need it? - Embedder