最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is this while loop not working with an AND logical operator? - Stack Overflow

programmeradmin0浏览0评论

I have an issue with a simple JavaScript program:

var answer = prompt("Are we there yet?");

while(answer != "yes" || answer != "yeah") {
    var answer = prompt("Are we there yet?");
}

alert("Yes! We made it!");

The problem is that when I run this on my browser and type either "yes" or "yeah" the prompt method keeps appearing unless I change the logical operator from OR (||) to AND (&&).

Shouldn't it work with OR (||)? Aren't I just saying that any of those two answers would be fine and the alert can run?

I have an issue with a simple JavaScript program:

var answer = prompt("Are we there yet?");

while(answer != "yes" || answer != "yeah") {
    var answer = prompt("Are we there yet?");
}

alert("Yes! We made it!");

The problem is that when I run this on my browser and type either "yes" or "yeah" the prompt method keeps appearing unless I change the logical operator from OR (||) to AND (&&).

Shouldn't it work with OR (||)? Aren't I just saying that any of those two answers would be fine and the alert can run?

Share Improve this question edited Nov 14, 2023 at 14:45 mkrieger1 23.6k7 gold badges64 silver badges82 bronze badges asked May 4, 2016 at 5:14 Kevin LiuKevin Liu 211 silver badge5 bronze badges 1
  • you are executing loop based on answer variable. value in answer variable is either "yes" or "yeah". both at same time not possible. but when you apply AND operator it check for answer value "yes" and "yeah" which is not possible. that's why your loop is not properly working – Krupesh Kotecha Commented May 4, 2016 at 5:22
Add a ment  | 

3 Answers 3

Reset to default 5

Your statement

answer != "yes" || answer != "yeah"

is true if either first or second condition is true. In your case regardless of the text in answer one of the conditions will always be true. For example:

  • if answer="yes" then second condition is true
  • if answer="yeah" then first condition is true
  • if answer="whatever" then both conditions are true

What you need to use instead is &&:

var answer = prompt("Are we there yet?");
while(answer != "yes" && answer != "yeah") {
    var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");

This will be true only when both conditions are met. For example:

  • if answer="yes" then first condition is false and second condition is true. Overall result is false and you leave the loop.
  • if answer="yeah" then first condition is true and second condition is false. Overall result is false and you leave the loop.
  • if answer="whatever" then both conditions are true and the loop is repeated.

Actually your condition checking is wrong.

here is your code snippet

 var answer = prompt("Are we there yet?");

 while(answer != "yes" || answer != "yeah") {
    var answer = prompt("Are we there yet?");
  }

  alert("Yes! We made it!");

In the above code snippet, while condition will always true whether it is "yes" or "yeah". you are binding two condition check with (||) OR Operator.

The (||) OR Operation is :-

true  || true = true
true  || false = true
false || true = true
false || false = false

So, (answar != "yes) || (answar != "yeah") will be true always either you type ("yes","yeah" or some other value).

The while loop will break only if condition return false but in above case it is always true. so it will create infinite loop.

lets try this :-

var answer = prompt("Are we there yet?");

while(answer != "yes" && answer != "yeah") {
    var answer = prompt("Are we there yet?");
}

alert("Yes! We made it!"); 

Due to (&&) AND Operation behavior,This will break infinite loop once any of value ("yes" or "yeah") given in prompt.

(&&) AND Operation behavior :-

true && false = false
false && true = false
false && false = false
true  && true = true 

So condition will be break once it contains any of value ("yes" or "yeah")

It needs to be an "and" because to get the loop, you need for the answer to be neither "yes" nor "yeah". If the tests were for equality rather than inequality, then you'd use the "or".

发布评论

评论列表(0)

  1. 暂无评论