LoginSignup
2
0

More than 3 years have passed since last update.

ド・モルガンの法則で条件をリファクタリング

Posted at

条件文をわかりやすく書きたいときに、ド・モルガンの法則が使えるパターンがある。

ド・モルガンは
「真と偽を入替え、論理和を論理積を入替えた論理体系」は、元の論理体系と同一視できる」という法則。

NOT(X) OR NOT(Y)

NOT(X AND Y)
と同じ

コードで書くと

if !a || !b


if !(a && b)

に書き換えられる。

NOT(X) AND NOT(Y)

NOT(X OR Y)
と同じ

コードで書くと


if !a && !b


!(a or b)

に書き換えられる。

2
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
2
0