tags:

views:

269

answers:

4

I have programmed in C, Pascal, GWBasic, TCL, Perl, Lisp, but Visual Basic is too advanced for me.

FOR j = 1 TO 31
  output_to_webpage "<p>Counter is " + j + "</p>"

  myDay = "" + j
  IF j < 10 THEN myDay = "0" + j

  MyStr = MyStr + ",j"
NEXT

The loop never appears to happen. Although if I comment out any references to the loop variable, j, it appears to loop.

How can I actually make Visual Basic loop. Or error. Not silently pretend there's no FOR loop there at all?

update: if the first statement of the loop was just a simple debugging statement I would expect it ALWAYS to be executed at least once, even if the rest of the loop was aborted. However, as pointed out below, the use of arithmetic on the loop variable somehow causes the entire loop not to be executed even once. Not even an initial debugging statement inside the loop. Very very strange I would think.

A: 

Have you tried NEXT j ?

Greg D
I tried NEXT J once; but i bit off more than i could handle. After that I was content with NEXT I ;)
Mitch Wheat
I'd delete this answer for being wrong, but I've decided to keep it just because Mitch's joke shouldn't disappear. :)
Greg D
+5  A: 

I think it's because you're adding strings using arithmic addition instead of string addition.

Code should be (from top of head):

FOR j = 1 TO 31
  output_to_webpage "<p>Counter is " & j & "</p>"

  myDay = "" & j
  IF j < 10 THEN myDay = "0" & j

  MyStr = MyStr & ",j"
NEXT
Sune Rievers
That was perfect. Well spotted, it fixed my loop. Why would using the result of a number cause the loop to simple not execute at all? Surely the first statement should always be executed?
PP
Logically, I would also assume that the loop was ran at least once, but then again, you never know with VBScript and dynamic typing :(It's probably something in the type system that detects `j` as a string insted of an integer, and renders the first test (the `FOR j = 1 TO 31`) to false (if treated like a while loop), so the for loop is never executed.
Sune Rievers
Good on you for answering what was a real question.
PP
Glad to help :)
Sune Rievers
@PP: Have you got a `On Error Resume Next` in your code ahead of your chunk. That would result in the seeming behaviour you describe. Since each line contains the same fault each will fail and fall through to the next line. Hence the loop will complete naturally having not actually done anything.
AnthonyWJones
Nice advice, AnthonyWJones, actually I do. I am maintaining old VBScript ASP pages and found myself tearing my hair out.
PP
A: 

It's more likely that the stuff inside the loop is broken. This works in a VB.NET console application:

For n = 1 To 31
    Console.WriteLine(n)
Next

So I doubt if For loops would be much different in previous versions of VB.

Try writing the body of your loop, substituting 1 for the loop variable, and see what it does.

Daniel Earwicker
A: 

Is j declared ?

Dim j as Integer

then add

Next j
daustin777