LoginSignup
1
2

More than 5 years have passed since last update.

[Unityで学ぶC#] 7 if - else

Last updated at Posted at 2017-03-04
1 / 11

if文をさらに使いこなそう


復習

if(条件){
  実行される処理
}

もし条件を満たしていれば処理を行う.

これがifの使い方です.


新しく学ぶこと

もし条件を満たしていれば処理Aを行う.
それ以外であれば処理Bを行う.


サンプル

スクリーンショット 2017-03-04 20.09.00.png

moneyが50より多ければ,ifの{}の中身が,それ以外であればelseの{}の中が実行されます.


サンプル

publicでint型を宣言しているため,Inspectorで値を変えることができます.
スクリーンショット 2017-03-04 20.12.34.png

スクリーンショット 2017-03-04 20.12.27.png


サンプル

値を49以下にすると,if文の条件を満たさなくなります
ifelse.gif
スクリーンショット 2017-03-04 20.12.16.png


まとめ

if (条件) {
    処理A // 条件を満たしていたら
} else {
    処理B // 条件を満たしていなかったら
}

おまけ

条件は追加することができます.

if (条件1) {
    処理A // 条件を満たしていたら
}else if(条件2){
        処理B // 条件1ではなく,条件2なら
} else {
    処理C // 条件を満たしていなかったら
}

{}が多くなるので, ペアを間違えないように注意しましょう.


おわり

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