0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

and / or の結果を変数に入れる時絶対注意!

Last updated at Posted at 2020-12-16

[x = true and false] は思ったとおりにならない

true and false # => false
false and true # => false
true and true # => true
false and false # => false

true && false # => false
false && true # => false
true && true # => true
false && false # => false

x = true and false # => false
p x # => true !!!!!!!!!!
x = false and true # => false
p x # => false
x = true and true # => true
p x # => true
x = false and false # => false
p x # => false

x = true && false # => false
p x # => false
x = false && true # => false
p x # => false
x = true && true # => true
p x # => true
x = false && false # => false
p x # => false

理由

  • and
  • or

=より優先順位が低いためx = true and falseでは

  1. x = true
  2. true and false (結果は捨てられる)

という現象が起きてしまい、xfalseにならない。

解決策

  • &&を使う
  • x = (true and false)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?