AllTheTimeWorld.com

csharp loopsA few notes about the while loop

A few notes about the while loop

Cautions about possibility of endless loops and loops that execute 0 times.

A few notes about the while loop

Cautions about possibility of endless loops and loops that execute 0 times.


This loop executes 0 times because num>0 is false to start:

int num = 0;
while (num >0)
{
    num++;
    lstNumbers.Items.Add(num);
}

This loop is an endless (or infinite) loop because num>0 is true to start, and keeps getting bigger, num>0 will always be true:

 int num = 1;
 while (num >0)
 {
   num++;
   lstNumbers.Items.Add(num);
}