while
using UnityEngine;public class L05 : MonoBehaviour{int cups = 4;void Start(){while(cups > 0){Debug.Log( "cups>0");cups --;}}}
有时,我们会使用值总是为true的表达式(例如布尔常量true)来计算,然后通过break语句来跳出循环。
int k = 0;while (true){System.Console.WriteLine(k);k++;if (k > 2) {break;}}
while和dowhile的区别
- while在循环主体前检验条件。
- do-while在循环主体结束时检验条件。这意味着do while至少会运行一次。
- while不使用;,do-while使用;。
do-while
```csharp using UnityEngine;
public class L05_1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
bool SC = false;
do
{
print(“hw”);
}
while(SC==true);
}
}
<a name="G0WjP"></a>## forfor语句以一个初始值开始,随后是每次循环都会计算的一个表达式,最后是表达式结果为true时会执行的一个语句块。每次循环迭代语句块执行之后,也会执行一条更新语句。```csharpusing UnityEngine;public class L05For : MonoBehaviour{// Start is called before the first frame updateint num = 3;void Start(){for(int i = 0; i<num; i++){Debug.Log("cr:" + i);}}}
