while文
-
ifと同じように真偽値の条件を使って繰り返しを行う。
- while(条件となる物){繰り返す処理}
- 繰り返す処理の中ではfalseになるようにする。インクリメント、デクリメントなど
//[while文] int max = 2; int total = 0; int count = 0; while ( count <= max) { total += count++; //totalに足した後、countに1を足す。 //total += ++count; //countに1を足した後、totalに足す。 //6になる } Message("合計は、" + total); //3
## for文
- while文をまとめたもの
- for( 初期処理; 条件となるもの; 繰り返し後の処理){処理内容}
- 条件となるものがfalseなら処理を抜ける。
```csharp
//[for文]
int max = 100;
int total = 0;
for(int i = 1; i <= max; i++)
{
total += i;
}
Debug.Log(max + "までの合計:" + total);
```