using System;
namespace demoapp
{
class LoopingStatements
{
public static void main(String[] args)
{
for (int i = 0; i <= 5; i++)
{
if (i == 4)
{
break; //this will break the loop
}
Console.WriteLine("The number is " + i);
Console.ReadLine();
} //control will jump here after the break statement.
}
}
}
这是带有 continue 语句的相同示例:
using System;
namespace demoapp
{
class LoopingStatements
{
public static void main(String[] args)
{
for (int i = 0; i <= 5; i++)
{
if (i == 4)
{
continue;// it will skip the single iteration
}
Console.WriteLine("The number is " + i);
Console.ReadLine();
}
}
}
}
我们可以在 C# 的循环中使用 continue 和 break 语句。使用 break 语句,我们可以中断循环执行,而使用 continue 语句,我们可以中断循环的一次迭代。下面是一个 break 语句的例子:
这是带有 continue 语句的相同示例: