Infinite Loop, but why?

# Thread: Infinite Loop, but why?

1. Member
Join Date
Dec 1969
Posts
34

## Infinite Loop, but why?

When I&#039;m comparing two numeric variables in my conditional of my DO WHERE loop, it creates an infinite loop. The code looks something like this:<BR><BR>total_q = request("total_q") &#039;in this test case total_q=12<BR>Questions = 1<BR>Do While Questions &#060;= total_q<BR>&#039;do stuff<BR>Questions = Questions + 1<BR>Loop<BR><BR>When I hard code my condional statement to say While Questions &#060;= 12, it works fine, but not when I use the other variable. both variables are getting the right values and incrementing properly. I&#039;m sorry if this is a very simple questions, but I am stuck. Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
449

## Well it appears that..

total_q may not = what you think...<BR><BR>total_q = request("total_q") &#039;in this test case total_q=12<BR>Response.Write total_q<BR>Questions = 1<BR>&#039;Do While Questions &#060;= total_q<BR>&#039;do stuff<BR>&#039;Questions = Questions + 1<BR>&#039;Loop<BR>

3. Junior Member
Join Date
Dec 1969
Posts
3

## RE: Well it appears that..

If you&#039;re requesting total_q from a form field, it may be looking at the 12 as a string instead of a number. Try changing your <BR><BR>Do While Questions &#060;= total_q <BR><BR>to <BR><BR>Do While Questions &#060;= cint(total_q)<BR><BR>I hope that works for you.

4. Member
Join Date
Dec 1969
Posts
34

## RE: Well it appears that..

I thought that it may see total_q as a string but it passes the isNumeric() test.

5. Member
Join Date
Dec 1969
Posts
34

## isNumeric() & response.write(total_q)

total_q passes the isNumeric(total_q) and response.write(total_q) gives the correct value. I&#039;m still not sure what is causing it...

6. Senior Member
Join Date
Dec 1969
Posts
449

## Do it anyway...

even if total_q is a string isnumeric will show true<BR>to prove it<BR><BR>total_q = request("total_q") &#039;in this test case total_q=12 <BR>total_q = int(total_q)<BR>Questions = 1 <BR>Do While Questions &#060;= total_q <BR>&#039;do stuff <BR>Questions = Questions + 1 <BR>Loop <BR>

7. Senior Member
Join Date
Dec 1969
Posts
2,334

## Try this code...

Convert the variable to an interger type,<BR><BR>total_q = request("total_q") &#039;in this test case total_q=12 <BR>Questions = 1 <BR>Do While Questions &#060;= CInt(total_q)<BR>&#039;do stuff <BR>Questions = Questions + 1 <BR>Loop

8. Member
Join Date
Dec 1969
Posts
34

## THANK YOU!!

after wracking my brain, it was that easy..Thanks!

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Why that works and other didn't

Just because the value passed the IsNumeric test doesn&#039;t mean that VBScript is going to convert it from string to number *FOR YOU*!<BR><BR>But not to ask a silly question: Why&#039;d you code it that way instead of the much simpler:<BR><BR>For Questions = 1 To CLNG(Request("total_q"))<BR> ...<BR>Next<BR><BR>Incidentally, I recommend using CLNG for all string-to-integer conversions, just as a force-of-habit thing. CINT is limited to numbers in the range -32768 to +32767, which is plenty in this case, but since CLNG will do the same thing with a much larger range, just use it and forget all about CINT.<BR><BR>(And INT() *will* work, but it&#039;s really a math function instead of a string-to-number conversion function.)<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
449

## works for me <eop>

.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•