Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advancing For loops

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US
Hello,

I have the following Script:

<%

For i = 1 to 100
a = a + 1
If a <> 50 then
Response.write(a)
Next
End If
Many More lines of code here

Next

What I want to do is step the for loop UP by one and start the For loop over if the varable "a" is NOT 50. Basically if "a" is NOT 50 then the code under the End If will not get executed and the for loop will increase by one and start over.

If "a" is equal 50 then the first next statement (under the Response.write(a) ) statment should be ignored.

If I place a NEXT statment under the "response.write(a)" statment then the server errors and says "unexpected Next"

Does anyone have a good idea on how to step up the for loop by one and start the for loop again when the "if" condition is true?

Thanks




 
You can't put the Next inside the If statement.

I'm not sure what you mean by your statement of starting the loop again, though. Do you mean you want your counter i to start again at 1, or just to begin the next iteration?

This looks a bit like homework, so if you could show some code that has real life applications, maybe we can help you better.

Lee

 
try this:

Code:
<%
a=1
for i=1 to 100
if a<>50 then
response.write a
response.write "<BR/>"
a=a+1
else
a=a+1
end if
next
%>

-DNG
 
You're not the first one to try that in VB, I ran into the same problem. Most other languages I've used allow you to do it, for some reason VB does not. Using an if-then-else is the only way around it.
Code:
For i = 1 to 100
  a = a + 1
  If a <> 50 then
    Response.write(a)
  Else
    Many More lines of code here
  End If
Next
If there are too many lines of code there to make it easy to follow, consider moving them into a sub or function and calling it there.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Why bother with the "a" when you can just use the loop iterator in the conditional?
Code:
For i = 1 to 100
  if i = 50 then 
    Response.Write i
  else
    'Other code goes here
  end if
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top