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.

C#:条件文

Posted at
  • if文

    • それが正しいかどうか判定する trueかfalseで判定

      • if(条件となるもの) { 正しい時に実行する処理内容 }
      • else if(条件となるもの) {正しい時に実行する処理内容} //必須ではない
      • else {上記の条件に当てはまらない場合の処理内容} //必須ではない 記載は一つまで
    • 条件の作り方

      • 基本的に比較演算子で作成
      • A == B, A != B, A > B, A >= B, A < B, A <= B
    • 偶数か奇数かの判断方法

      • x = 6の場合 条件分を x % 2 == 0 とする。
    //[if文]
    
    /* if(条件となるもの truefalseで判定できるもの)
    {
      正しい時の処理
    }
    else
    {
      正しくない時の処理
    }
    

 */

//例
int x = 12345;
string s = "「" + x + "」は、";

if(x % 2 == 0)
{
  //変数宣言を上記ではなくここですると変数宣言範囲によりエラーが起きる
  s += "偶数です。";
}
else
{
  s += "奇数です。";
}
Debug.Log(s);
```
  • switch文

    • 複数の分岐を作るときに使用
      • switch(チェックする値){case 値: 処理内容 break;}
      • 処理内容の後にはbreak;をつける つけない場合は、caseが一致したところから、break;がある部分まで、処理が実行される。
      • 最後はcase 値の部分を default にする 不要ならばつける必要はなく、caseが見つからなければ実行せず構文を抜ける。
    //[switch文]
    int month = 1;
        string s = "「" + month + "月」は、";
    
        switch (month)
        {
            case 1:
                s += "睦月";
                break;
            case 2:
                s += "如月";
                break;
            case 3:
                s += "弥生";
                break;
            default:
                s += "何だかわからない";
                break;
        }
        s += "です";
        Debug.Log(s); //「1月」は、睦月です
    
0
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
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?