while 陳述式會重複執行陳述式或陳述式區塊,直到指定的運算式評估為 false 為止。
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public int i; void Start () { Plus (); } void Update () { } void Plus(){ while(i<10){ i++; } } }
所以i被加到10之後,迴圈變會跳出結束。
或者如下寫法會得到相同結果:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public int i; // Use this for initialization void Start () { Plus (); } // Update is called once per frame void Update () { } void Plus(){ while(true){ i++; if (i == 10) break; } } }
用break;中斷迴圈的動作。
也就是說while是一旦達成條件便會結束的迴圈