1
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?

三項演算子の書き方

Posted at

三項演算子は if-then-else の式バージョンである。

if-then-else なら入れ子の場合、

if (条件A) {
    (条件Aが真の場合)
}
else {
    if (条件B) {
        (条件Bが真の場合)
    }
    else {
        (その他の場合)
    }
}

などと書かずに

if (条件A) {
    (条件Aが真の場合)
}
else if (条件B) {
    (条件Bが真の場合)
}
else {
    (その他の場合)
}

と書く。

ならば三項演算子でも

rv = (条件A) ? (条件Aが真の場合)
   : (条件B) ? (条件Bが真の場合)
   : (その他の場合); 

と書けばいい話である。

これが判っていないから「三項演算子禁止論」なんてものが出てくるのだ。(優先順位のバグでこう書けない壊れた言語PHPや、これを崩してしまう頭の悪いフォーマッタのせいでもあるだろうが)

1
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
1
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?