AllTheTimeWorld.com

csharp loopsPosition of increment in loop

Position of increment in loop

The position of the increment changes the values and also what is added to list box

Position of increment in loop

The position of the increment changes the values and also what is added to list box

Note: If the Boolean expression is true, all of the statements in the loop execute, even if the expression becomes false during the loop.
Look at the difference in the output when the position of the increment is before adding to the list box:
powers

private void Form1_Load(object sender, EventArgs e)
{
  int num = 1;
  while(num<1000)
  {
     num *= 2;
     listBox1.Items.Add(num);
  }
}

A common mistake with loops is to omit the first value and include an extra value past the intended end point.