Zebra0.com

csharp loops

A common mistake with loops is to create an endless loop. In the example a line to subtract 1 from i is missing and the loop will never end.

private void Form1_Load(object sender, EventArgs e)
{
  // Add 10,9,8,7,6,5,4,3,2,1,Blast OFF! to a list box.
  int i = 10;
  do
  {
    listBox1.Items.Add(i);
  } while (i > 0);
  listBox1.Items.Add("Blast off");
}

Depending on what else the loop is doing, the system may eventually give you an error message, or you will realize the program is “frozen” or “hung-up.”
If you are working in Visual Studio, you can select Debug,Stop Debugging from the menu to end the program.

End of lesson, Next lesson: