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?

More than 5 years have passed since last update.

(3)- if文の実験

Last updated at Posted at 2020-04-23

やりたかった事

if文でつまづいた。

「if」
「else if 」
「else」

なんでいうこと聞いてくれんのやろか?
理解出来ていないところを探してみようかn。。

通常のif文から実験してみる

1)「if」のみ

    void Experiment()
    {
        if (true)
        {
            Debug.Log("アジング");
        }
    }

01.JPG

--

2)「if」+「else if 」

    void Experiment()
    {
        if (false)
        {
            Debug.Log("アジング");
        }
        else if (true)
        {
            Debug.Log("エギング");
        }
    }

02.JPG

--

3)「if」+「else if 」2 (アジングにもエギングにも行きたい場合)

    void Experiment()
    {
        if (true)
        {
            Debug.Log("アジング");
        }
        else if (true)
        {
            Debug.Log("エギング");
        }
    }

01.JPG

これ、ありえないパターンなのか。(行けない)
ifでもしも(条件1)、、、
else ifでそうでなくて(条件2)
条件1が満たされなかったときの条件2発動的なやつか

--

4)結果、ガチジギングになった場合。

    void Experiment()
    {
        if (false)
        {
            Debug.Log("アジング");
        }
        else if (false)
        {
            Debug.Log("エギング");
        }
        else
        {
            Debug.Log("ガチジギング");
        }
    }

03.JPG

elseはそれ以外的な。

--

ここから本題

5)あたまにもう一つ「if」をつけてみた

    void Experiment()
    {
        if (true)
        {
            if (true)
            {
                Debug.Log("アジング");
            }
            else if (false)
            {
                Debug.Log("エギング");
            }
            else
            {
                Debug.Log("ガチジギング");
            }
        }
    }

01.JPG

↓↓↓
これも不可。最初のifがfalse
最初のifがtrueでも、その下にtrueは1通りしか認められない。

    void Experiment()
    {
        if (false)
        {
            if (true)
            {
                Debug.Log("アジング");
            }
            else if (false)
            {
                Debug.Log("エギング");
            }
            else
            {
                Debug.Log("ガチジギング");
            }
        }
    }

--

6)もう一つ入れ子の中に「if」をつけてみて

    void Experiment()
    {
        if (true)
        {
            //もしも
            if (true)
            {
                Debug.Log("アジング");
            }

            if (true)
            {
                Debug.Log("カサゴ狙い");
            }

            else if (false)
            {
                Debug.Log("エギング");
            }

            else
            {
                Debug.Log("ガチジギング");
            }
        }
    }

04.JPG

これはいけるくち

--

まとめると

入れ子のOKパターンは

1)最初のifがtrueでその下もtrueが1個####

  • if (true)

  • if (true)

  • else if (false)

  • else

  • if (true)

  • if (false)

  • else if (true)

  • else

  • if (true)

  • if (false)

  • else if (false)

  • else

2)最初のifがtrueでその下にifが2個####

  • if (true)
  • if (true)
  • if (true)
  • else if (false)
  • else

入れ子のNGパターン

1)最初のifがfalse####

  • if (false)
  • if (true)
  • else if (false)
  • else

2)最初のifがtrueでも、その下trueが2個

  • if (true)
  • if (true)
  • else if (true)
  • else

ちなみに「}」の位置変えたら

if **{}**if **{}**と、if **{**if **{}}**は結果がどう違うのか?を確認する実験。

    void Experiment()
    {
        //
        if (true)
        {
            Debug.Log("アジング");

            if (true)
            {
                Debug.Log("カサゴ狙い");
            }
        }
    }

これは通って同じ結果になるけど

    void Experiment()
    {
        //
        if (true)
        {
            Debug.Log("アジング");

            else if
            {
                Debug.Log("カサゴ狙い");
            }
        }
    }

else ifやelseは「とたんに」通らなくなる。

最後に

    void En100()
    {
        //100円の処理
        for (int j = 500; j >= 0; j--)
        {
            //もし「j」を100で割れるなら
            if (j%100==0)
            {
                //もし「j」が500ならallAnsorに+1
                if (j == 500)
                {
                    allAnsor++;
                }
                //それ以外の時は、CalcJに値を代入する。
                else
                {
                    CalcJ = j;
                }
            }
        }
    }

これが、ゆうこときかないやつだったのですけど
ifの時もelseの時も、両方結果が欲しかった。
なんでelseで0が返ってくるのか?から始まった。

両方結果欲しいんなら、okパターンの(2)必殺入れ子if ifで解決できるかも。

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?