在C#执行for循环可以用continue与break进行跳过
continue表示跳过当前循环,进行下一次循环;
break表示结束当前循环;
事例:
continue测试:
public class Test: MonoBehaviour
{
float _num = 0;
void Start()
{
for (int i = 0; i < 5; i++)
{
_num++;
if (_num == 3)
{
continue;
}
Debug.Log("_num:" + _num);
}
}
}
输出结果为:
break测试:
public class Test: MonoBehaviour
{
float _num = 0;
void Start()
{
for (int i = 0; i < 5; i++)
{
_num++;
if (_num == 3)
{
//continue;
break;
}
Debug.Log("_num:" + _num);
}
}
}
输出结果为:
结论:break少了两次输出,当运行到break时结束循环。