A plurality of loop condition problem (logical AND, logical OR, operator precedence)

 Loop set a number of conditions, sometimes not getting what the desired result, here is my clear priority for operators, due to the wrong summary:

1 <Script of the type = "text / JavaScript">
 2      var str = prompt ( "Do you love me?" );
 3      the while (! Str = "I love you" & "I love you!" ) {
 4          str = prompt ( "do you love me?" );
 5      }
 6 </ Script>

Want to enter, "I love you" and "I love you!" Can be the end of the cycle.

But! = Higher precedence than &&, ||, the above code will first execute str! = "I love you", if it is ture, was true && "I love you!" To return to "I love you!" "I love you!" as ture, the cycle continues. If the front is false (type "I love you"), false && "I love you!", Returns false, the loop ends. That second "I love you!" Has been true, does not make sense. The logic input is only "I love you" cycle to end.

PS: (0, "", ", null, undefined, NaN is false, the rest are true)

change into:

1 <Script of the type = "text / JavaScript">
 2      var str = prompt ( "Do you love me?" );
 3      the while (! Str = "I love you" && str = "I love you!"! ) {
 4          str = prompt ( "do you love me?" );
 5      }
 6 </ Script>

To perform two! =, And then && operator. Two conditions can be used. Logically that is, enter "I love you" or "I love you!", You can end the cycle.

Reanalysis of the logical OR:

<Script of the type = "text / JavaScript">
      var str = prompt ( "Do you love me?" );
      the while (! "! I love you" str = "I love you" || ) { 
         str = prompt ( "you I love you "? ); 
     }
 </ Script>

If the input other than "I love you", the true || "I love you!", Returns true, the loop continues. If you enter "I love you", the false || "I love you!" To return to "I love you!", The result was true, the cycle continues. Logically, no matter what you type, the cycle will not stop.

<Script of the type = "text / JavaScript">
      var str = prompt ( "Do you love me?" );
      the while (!! str = "I love you" || str = "I love you!" ) { 
         str = prompt ( "do you love me?" ); 
     }
 </ Script>

Here is the logic, only the input value is "I love you", and for "I love you!" To end the cycle. The string does not have one, so there is no sense.

Knowledge summarized as two points:

  1. 0, "", ", null, undefined, NaN Boolean value false, the rest are true

  2. JS operator precedence  , "! =" && again higher priority than || 

priority Operation type Relevance Operators
20 Parentheses n/a ( ... )
19 Member Access From left to right ... . ...
19 Member Access to be calculated From left to right ...[ ... ]
19 new (with the parameter list) n/a new ... ( ... )
19 Function call From left to right ... ( ... )
18 new (no parameter list) From left to right new ...
17 Postincrement (after operator) n/a ... ++
17 Post-decrement (operator after) n/a ... --
16 Logical NOT From right to left ! ...
16 Non-bit From right to left ~ ...
16 Adding one yuan From right to left + ...
16 Unary minus From right to left - ...
16 Pre-increment From right to left ++ ...
16 Pre-decrement From right to left -- ...
16 typeof From right to left typeof ...
16 void From right to left void ...
16 delete From right to left delete ...
15 power From right to left ... ** ...
14 multiplication From left to right ... * ...
14 division From left to right ... / ...
14 Modulo From left to right ... % ...
13 addition From left to right ... + ...
13 Subtraction From left to right ... - ...
12 Bitwise left shift From left to right ... << ...
12 Bitwise Right Shift From left to right ... >> ...
12 Unsigned right shift From left to right ... >>> ...
11 Less than From left to right ... < ...
11 Less than or equal From left to right ... <= ...
11 more than the From left to right ... > ...
11 greater or equal to From left to right ... >= ...
11 in From left to right ... in ...
11 instanceof From left to right ... instanceof ...
10 equal sign From left to right ... == ...
10 Non-equal sign From left to right ... != ...
10 Full equal sign From left to right ... === ...
10 Part-equal sign From left to right ... !== ...
9 Bitwise AND From left to right ... & ...
8 Bitwise XOR From left to right ... ^ ...
7 Bitwise or From left to right ... | ...
6 逻辑与 从左到右 ... && ...
5 逻辑或 从左到右 ... || ...
4 条件运算符 从右到左 ... ? ... : ...
3 赋值 从右到左 ... = ...
3 赋值 从右到左 ... += ...
3 赋值 从右到左 ... -= ...
3 赋值 从右到左 ... /= ...
3 赋值 从右到左 ...% = ...
3 赋值 从右到左 ... *= ...
2 yield 从右到左 yield ...
2 yield* 从右到左 yield* ...
1 展开运算符 从左到右 ... ...
0 逗号 从左到右 ... , ...

  

 

Guess you like

Origin www.cnblogs.com/nc9527/p/11444296.html