首頁  >  文章  >  後端開發  >  繼續使用 C#

繼續使用 C#

WBOY
WBOY原創
2024-09-03 15:09:44631瀏覽

Continue 是C# 程式語言中可在條件循環區塊內使用的眾多條件語句之一,它可以用作子句,在執行迭代條件後繼續循環執行,以便繼續執行條件循環中的下一次迭代的執行。它通常與迭代條件迴圈一起使用,例如 for-while 迴圈、do-while 迴圈和 for-each 迴圈。

Continue 語句在 C# 中如何運作?

在下圖中,當迴圈開始時,如果有 continue 語句,它將停止目前迭代,並透過返回到迴圈開頭將控制權傳遞給下一個迭代。

流程圖

下面是 continue 語句的流程圖,顯示了它是如何實現的。

繼續使用 C#

下面的範例展示了它如何與 for、while、do-while、foreach 和內循環等循環體一起使用:

範例#1

a。 在下面的範例中,使用了 for 迴圈。當變數的值等於 5 時, continue 語句將跳過目前迭代並跳到下一次迭代以顯示該值。

using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
for(int x=1; x<=6; x++ )  // loop runs six times
{
if (x == 5)  //  value is equal to 5
continue;     // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

輸出:

繼續使用 C#

b。 在下面的範例中,當變數的值小於 6 時,它將跳過迭代並跳到值等於或大於 6 的下一個迭代並顯示值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
for(int x=1; x<=10; x++ )   // loop runs ten times
{
if (x < 6)     // values less than six
continue;      // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

輸出:

繼續使用 C#

c. 在下面的範例中,循環運行十次,並在變數x 為奇數時跳過迭代,並將控制權傳遞給下一次迭代,並在變數x 為偶數時列印變數x 的值。這就是我們如何使用 continue 語句列印偶數系列。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
for (int x = 2; x <= 10; x++)   // loop runs ten times
{
if (x % 2 == 1)   // logic to print even number
{
continue;   // skips the iteration
}
Console.Write("{0} ", x);
}
Console.ReadLine();
}
}
}

輸出:

繼續使用 C#

範例#2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
int x = 0;   // initializing variable
while(x < 7) // loop runs seven times
x++;   // incrementing the value of x
if(x==5)   // value is equal to 5
continue; // skips the iteration
Console.WriteLine("value is :{0}", x);
}
Console.ReadLine();
}
}
}

在上面的例子中,使用了while循環。變數x被初始化。當 x 的值等於 5 時,使用 continue 語句跳過迭代並顯示其他值。

輸出:

繼續使用 C#

範例#3

a。 在下面的範例中,使用了 do while 迴圈語句。變數 x 被初始化,當 x 的值等於 4 時, continue 語句停止迭代並將控制權交給下次執行並顯示值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
static void Main(string[] args)
{
int x = 1; // initializing variable
do
{
Console.WriteLine("value is :{0}", x);
x++;  // incrementing the value of x
if (x == 4)
continue;  //  skips the iteration
} while(x < 6) ;
Console.ReadLine();
}
}
}

輸出:

繼續使用 C#

b。 在下面的範例中,使用了 while 循環。變數x被初始化。當 x 的值等於 8 時,使用 continue 語句跳過迭代並顯示其他值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
int x = 8;   // initializing variable
do
{
if (x == 13)
{
x = x + 1;
continue;   // skips the iteration
}
Console.WriteLine("a: {0}", x);
x++;      // incrementing the value of x
}
while (x < 15);
Console.ReadLine();
}
}
}

輸出:

繼續使用 C#

範例#4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
for (int x = 1; x <= 4; x++)                      // loops run four times
{
for (int y = 1; y <= 4; y++)
{
if (x == 3 && y == 3)
{
continue;                                     // skips the iteration
}
Console.WriteLine(x + " " + y);
}
Console.ReadLine();
}
}
}
}

在上面的範例中,內部迴圈內部使用 continue 語句根據變數 x 和 y 的值跳過迭代。

輸出:

繼續使用 C#

範例#5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContinueExample
{
class Demo
{
public static void Main(string[] args)
{
int[]arr = { 2, 4, 25, 34, 28, 57};          // initializing array
foreach (int a in arr)                       // iteration
{
if (a == 25)                              //Array element value equal to 25
{
continue;                            // skips the iteration
}
Console.WriteLine(a);
}
Console.ReadLine();
}
}
}

上面的範例中使用了foreach進行迭代。初始化一個元素的數組,該數組由六個元素組成。當變數等於 25 時, continue 語句將跳過迭代並將控制權傳遞到下一次迭代並顯示值。

輸出:

繼續使用 C#

結論

這就是我們如何將 continue 語句與不同的迴圈體(如 for、foreach、while、do-while 等)一起使用,並根據條件跳過迭代。大多數 continue 語句與 for 和 foreach 迴圈體一起使用。

以上是繼續使用 C#的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的 Else If下一篇:C# 中的 Else If