C#のdo~while文を使った繰り返し処理を紹介します。
do{
...(処理)
}while (条件式)
条件式が真(true)である限りブロック内の処理を実行し続けます。while文との違いは条件式の判定はループ内の処理が実行された後にされるため、条件式が偽(false)の場合でも1回はループ内の処理が実行されます。
private void button9_Click(object sender, EventArgs e)
{
int a=0;
do {
textBox1.Text += a.ToString() + " ";
a++;
} while (a < 10);
}
0 1 2 3 4 5 6 7 8 9
ループの条件式は最初から偽(false)ですが、ループの内部の処理が1回実行されています。
private void button10_Click(object sender, EventArgs e)
{
int a=0;
do {
a++;
textBox1.Text += a.ToString() + " ";
} while (a < -5);
}
1