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.

if文で論理演算子をたくさん使いたいの巻

Last updated at Posted at 2020-10-15

はじめに

 本日学んだ、if文で条件を重ねたいときに、論理演算子をどう使うかについてまとめる。

論理演算子

&&→かつ

if a && b
# 処理
end

aとbの両方がtrueのとき、処理が実行される。

||→または

if a || b
# 処理
end

aかbのどちらかがtrueであれば処理が実行される。

!a
trueとfalseが反対になる。

if !a
# 処理
end

aが本当はfalseだと、処理が実行される。(falseがtrueに変わるため)

複数の論理演算子をif文で使う

簡単に言ってしまえば、( )を使って、まとまりを括る。


if ( a && b ) || ( !a && !b )
# 処理
end

||があるため、左辺か右辺がtrueであれば、処理が実行される。
左辺は「aかつbがtrue」であれば、左辺全体としてtrueになる。
右辺は「aかつbが本当はfalse」であれば、右辺全体としてtrueになる。
要は、aとbがtrueかfalseで一致してれば、処理が実行されるというコード。

さいごに

高校の数Aでやったような内容だなぁ…

0
0
2

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?