2

I am curious as to why this while loop is not breaking as expected. It is my understanding that when using the OR operator (||), if any of the conditions are found to be false, the loop breaks.

However, I noticed in a test document (I am learning JS for the first time) that my while loop is requiring ALL conditions to be found false before breaking.

This is a stub example:

var stringName="", numhours=0, numRate=0;

document.write("Exit at any time by entering -1");
while (stringName != "-1" || numHours != -1 || numRate != -1){
    stringName = prompt("Enter name","");
    numHours = prompt("Enter num hours","");
    numHours = parseFloat(numHours);
    numRate = prompt("Enter rate per hour","");
    numRate = parseFloat(numRate);
}

I would like it so that a user can enter -1 at any stage, and once the loop completes, it will re-check the conditions again, and after finding that the it is false that the name does not equal "-1", or false that the hours does not equal -1, or that it is false that the wage does not equal -1, it will break the loop.

Instead, it seems the loop is requiring that all of those conditions are met to exit, where name == "-1" AND hours == -1 AND wage == -1, only then will it break.

Any insight would be appreciated. You guys rock!

  • Possible duplicate of [Javascript || operator](http://stackoverflow.com/questions/1378619/javascript-operator) – 10 Replies Feb 04 '16 at 03:25

3 Answers3

7

You have your understanding of the OR operator backwards. You're treating it like AND.

Instead of:

It is my understanding that when using the OR operator (||), if any of the conditions are found to be false, the loop breaks.

It's actually

If any of the conditions are found to be true, the loop continues.

To fix the problem you can rewrite your loop to be:

while (stringName !== "-1" && numHours !== -1 && numRate !== -1)

Also it is preferable to use !== instead of != because != does something weird called type coercion that can give you results you don't expect.

m0meni
  • 16,006
  • 16
  • 82
  • 141
  • 1
    This helps me understand my mistake. All of your answers are on the money, by the way, but AR7 really helped me understand the reason a bit better. – Matthew Ryan Feb 04 '16 at 03:21
  • @AR7 Can you provide an example where != does weird thing? Thanks – Viet Nguyen Feb 04 '16 at 03:36
  • @VietNguyen sorry didn't see this earlier. Take a look at http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – m0meni Feb 04 '16 at 04:22
  • @AR7 Perhaps starting coding from C++ prevent me from seeing the weird thing as it is perfectly normal for == op as per your link's answers whilst === is a little more strict on type conversion that could give me an unexpected result such as 0===false is false which is weird for me. Thanks anyway. – Viet Nguyen Feb 04 '16 at 04:39
4

You have it backwards. The || operator evaluates to true if either operand has a "truthy" value (true, non-null, etc.). Your while loop will exit only when all the individual inequality tests are false. If you want it to exit when any of them is false, use the && operator instead.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

while(condition) will loop until condition == false.

Your question can be answered when we test this using the console: true||false||false||false||true will evaulate to true

since OR statements evaluate to true if any one of their conditions are true.

Logical OR Operator

Redmega
  • 673
  • 5
  • 21