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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@AR7 Can you provide an example where != does weird thing? Thanks
@VietNguyen sorry didn't see this earlier. Take a look at stackoverflow.com/questions/359494/…
@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.
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.

2 Comments

Thank you, too. This is very helpful. I don't recall C or Java working this way...
@TedHopp, thank you for clarifying. I now find myself wondering how I've gotten this far. lol. You fellas have been very helpful.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.