C#中while循環(huán)的基本語(yǔ)法是什么

c#
小樊
83
2024-08-29 20:49:36

在C#中,while循環(huán)的基本語(yǔ)法如下:

while (condition)
{
    // 代碼塊
}

其中,condition 是一個(gè)布爾表達(dá)式,用于測(cè)試循環(huán)條件。當(dāng)條件為 true 時(shí),執(zhí)行循環(huán)體內(nèi)的代碼塊。當(dāng)條件為 false 時(shí),跳出循環(huán),繼續(xù)執(zhí)行循環(huán)之后的代碼。

以下是一個(gè)簡(jiǎn)單的示例:

int counter = 0;

while (counter < 5)
{
    Console.WriteLine("Counter: " + counter);
    counter++;
}

這段代碼將輸出:

Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4

0