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.

[Unity初心者]for文"i"の正体 "i"の有効範囲

Last updated at Posted at 2020-04-09

環境

Unity 2019.3.7f1

"i"の正体

簡単にいうと"i"はfor文の中で宣言した単なる変数です!
aで宣言しても、bで宣言しても動きます。

勉強を始めたばっかりだと、
for文を使うといえば慣習的に"i"を使っている場合が多いと思います。
"i"を使わないといけないってことはないのです。
でも、共通の認識を合わせる目的で"i"を使ったほうが無難だと思います。

"i"の有効範囲

for文の中で宣言しているのでそのfor文の中で有効です。
(詳しくは"C# 変数 スコープ"で検索)





うまく説明できないので
とりあえず、次の2つに気を付けてfor文を使いましょう。笑
・単発的にfor文を使う分には変数"i"をそれぞれ宣言する形でコード上は問題無い。
 (後で見た時、混乱する恐れがあるので変数名は変えたほうが無難?)
・for文の中にfor文を入れるときは変数名は変える

以下コード付きの例です。

まずはfor文の基本

for文:繰り返し処理する関数

↓for文を使った例と結果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private int a;//int型の変数aを宣言

    //スタート関数
    void Start()
    {
        a = 0;//aの初期値設定


        //繰り返し処理
        for (int i = 0; i < 3; i++)
        {
            a = a + 1;//aに1を足す
            Debug.Log("a="+a);//aの中身をコンソールに表示
        }
    }

}

image.png

for文を簡単に解説
image.png

"i"の有効範囲解説

単発的にfor文を使う分には変数"i"をそれぞれ宣言する形でコード上は問題無い。

先程の例文に加えてスタート関数内にfor文を追加します。
例えるなら、同じ名前の人が2人いるイメージ(わかりにく笑)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private int a;//int型の変数aを宣言
    private int b;//int型の変数bを宣言

    //スタート関数
    void Start()
    {
        a = 0;//aの初期値設定


        //繰り返し処理
        for (int i = 0; i < 3; i++)
        {
            a = a + 1;//aに1を足す
            Debug.Log("a="+a);//aの中身をコンソールに表示
        }

        //for分追加
        //繰り返し処理
        for (int i = 0; i < 5; i++)
        {
            b = b + 2;//bに2を足す
            Debug.Log("b="+b);//bの中身をコンソールに表示
        }
    }

}

結果
image.png

for文の中にfor文を入れるときは変数名を変える

NG例
1つ目と2つ目のfor文で変数宣言"i"が重複している
例えるなら自分の2人の子供に同じ名前をつけるイメージ (またわかりにく笑)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private int a;//int型の変数aを宣言
    private int b;//int型の変数bを宣言

    //スタート関数
    void Start()
    {
        a = 0;//aの初期値設定


        //繰り返し処理
        for (int i = 0; i < 2; i++)
        {
            a = a + 1;//aに1を足す
            Debug.Log("a="+a);//aの中身をコンソールに表示

            for (int i = 0; i < 2; i++)
            {
                b = b + 1;//bに1を足す
                Debug.Log("b=" + b);//bの中身をコンソールに表示
            }
        }

    }

}

成功例
2つ目のfor文の中の変数は"j"を使っている

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private int a;//int型の変数aを宣言
    private int b;//int型の変数bを宣言

    //スタート関数
    void Start()
    {
        a = 0;//aの初期値設定


        //繰り返し処理
        for (int i = 0; i < 2; i++)
        {
            a = a + 1;//aに1を足す
            Debug.Log("a="+a);//aの中身をコンソールに表示

            for (int j = 0; j < 2; j++)
            {
                b = b + 1;//bに1を足す
                Debug.Log("b=" + b);//bの中身をコンソールに表示
            }
        }

    }

}

結果
image.png

とりあえず

for文の変数は都度変えて使い分けた方が無難

1
0
4

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?