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 1 year has passed since last update.

言語別 関係・論理演算子 ==と===の違いのまとめ 2023/5/6

Last updated at Posted at 2023-05-06

そんなのまとめなくてもわかる。という方しかほぼいないと思いますが、なんかわけわからなくなることがたまにあったりなかったりするので整理
言語: C, Java, JavaScript, Python

一応、関係演算子(比較演算子)・論理演算子とは

  • 関係演算子: あるものとあるものの関係を調べようぜ、の時に使う。true/falseの真偽値で返す。
    例: AとBが同じとき,AがBより大きいとき,AがB以下のときみたいな感じで使う
  • 論理演算子、命題的な感じでの論理演算の演算子の総称
    例: AとBが同じかつCがDより大きいまたはEがF以下のときに~~みたいな感じで使う

真偽値

言語 その他に偽として判定される値(falsyな値)
C false true 0, NULL
Java false true なし
JavaScript false true undefined, null, 数値の0, 空文字列
Python False True None, 数値の0、空文字列, 空リスト, 空タプル, 空辞書

等号/不等号

言語 等号 不等号
C == !=
Java == !=
JavaScript == !=
Python == !=

大/小/以下/以上

言語 以下 以上
C < > <= >=
Java < > <= >=
JavaScript < > <= >=
Python < > <= >=

かつ/または/否定

言語 かつ または 否定
C && || !
Java && || !
JavaScript && || !
Python and or not

まとめてみるとほぼみんないっしょ。これじゃ中身が薄いから、==と===の違いについて

==と===の違いについて

JavaScriptには==と===があり、これらは混ぜて使われがちで議論を生みます。
==とは、等価演算子であり、文字列と数字を比較するときには文字列を数字に変換してから比較します。
===とは、厳密等価演算子であり、まったく、形式を変換せずに比較します。
厳密にやるなら===ということですね。

言語 値型同値 参照型同値 参照型同一 曖昧等価 厳密等価
C == 各種比較関数 == なし なし
Java == equalsメソッド == なし なし
JavaScript なし なし なし == ===
Python == == is なし なし

まとめ

演算子はいろいろあるから理解して使うようにします。

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?