while (条件式){
...(処理)
}
C#でwhile文による繰り返し処理のコードと実行結果を紹介します。
while (条件式){
...(処理)
}
条件式が真(true)である限りブロック内のループ処理を実行し続けます。条件式が偽(false)であった場合は、ループ内部の処理は一度も実行されません。
private void button4_Click(object sender, EventArgs e)
{
int a=0;
while (a < 20) {
textBox1.Text += a.ToString() + " ";
a++;
}
}
下記のように条件式部分に "true"と記述すると無限ループになります。whileループ内でbreakやreturnなどでループを抜ける処理が必要になります。
while(true){
...(処理)
}
private void button5_Click(object sender, EventArgs e)
{
int a=0;
while (true) {
textBox1.Text += a.ToString() + " ";
a++;
if (10 < a) break;
}
}