While loop help

vickp07

Member
Joined
Sep 8, 2009
Messages
7
Programming Experience
3-5
I am writing a program that when a user enters his initial age: lets say 23, and then in another text box he enters in his age on retirement: lets say 86, I want to display data as show below:

24
25
26
27
28
29
.
.
.
86

I am currently using this code but it is not working the way i wanted it to can someone please tell me what i am doing wrong? It just keeps counting and counting and never stops at 86 if that is what the user enters in the Age on Retirement textbox. Please help?

Dim iEndAge As Integer = 0

Dim iCount As Integer = txtAge.Text 'used to increment the initial age

While iEndAge <= txtOldAge.Text

iCount = iCount + 1 'increment Initial Age


rtbDisplay.Text = rtbDisplay.Text & vbNewLine & vbNewLine & iCount

End While
 
First, turn on Option Strict, and second, a for loop seems to be more fitting for this purpose:

VB.NET:
For i As Integer = Convert.ToInt32(Me.txtAge.Text) To Convert.ToInt32(Me.txtOldAge.Text) Step 1
      Me.rtbDisplay.AppendLine(i.ToString("N0"))
Next

And your while loop is looping forever 'cause you never set iEndPage. ^^

Bobby
 
You use iEndAge to control the loop, yet you increment iCount, so iEndAge is always = 0 throughout the loop.
 
Back
Top