A common mistake with loops is to omit the first value and include an extra value past the intended end point.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Num As Integer
lstNumbers.Items.Add("Powers of 2")
Num = 1
While Num < 1000
Num = Num * 2
lstNumbers.Items.Add(Num)
End While
End Sub
Occasionally, you create a loop that will never end. In the example above, if you leave out the statement Num=Num*2, Num starts out as 1 and stays 1. The statement Num<1000 will always be true. 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 VB, you can click the black square on the toolbar to end the program.