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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advancing a For Statement inisde a IF Statment 1

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US
Hello,

I have the following code:

For i = 0 to 100
For j = 0 to 5
If i = 0 AND j=0 then
Next j
End If
Response.write (j)
Response.write (i)
Next
Next

The object here is to not display i & j when they are BOTH equal to 0. All I would like to do here is if i and j are both equal to 0 then the "For j" loop will start again with j = 1
.

The above as it shows will give the error: "Unexpected Next Statement"

Can anyone help?

Thanks.


 
i am not completely sure what you are trying to do here. You shouldnt say "next j"

Code:
For i = 0 to 100
  For j = 0 to 5
    If i = 0 AND j=0 then
      [red]j=j+1[/red]
    End If
    Response.write (j)
    Response.write (i)
  Next
Next

-DNG
 
...Or maybe this?

For i = 0 to 100
For j = 0 to 5
If i+j > 0 then
Response.write (j)
Response.write (i)
End If
Next
Next

BDC.
 
I like BDC2's answer, very elegant.

barcode_1.gif
 
I don't find the answer elegant, I find it confusing. Since the value i+j has no real meaning, using it in the if statement just confuses things. That if statement should read:

if i <> 0 or j <> 0 then

Which does the same thing, but shows whoever is reading the code what you are really trying to test for.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
except that we know that zero is the lowest value of i and j so its not like you could end up with -2 + 2 = 0 or somesuch.
 
i dint delve into the details of what the loop was doing...just pointed out that "next j" was wrong syntax ;)

-DNG
 
I think "Next J" was the syntax used on my old C-64 and Apple IIe
 
Incrementing a loop counter inside a for loop is usually considered risky programming.

You can't split block level statements like the original example shows. Loops, conditionals, and Subs and Functions are all blocks and must be used in pairs with the beginning and end of the block at the same level.

Lee
 
Sheco: whether or not i or j could be less than zero is irrelevant. The point I was trying to make is that i and j are loop variables, and the value i+j has no real meaning. While using i+j in the if statement will work, it totally obfuscates (confuses) the meaning of the if statement. That's just bad programming practice. Other than that, BDC2's code is fine.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
how about taking it farther and looking at how ugly and poor performing it is period to use the For Next Loop let alone a nested one within another?

It starts there and causes this conversation to become a conversation in the first place. Isn't that why god created better methods and looping structures in the first place?


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
see what you started Krus1972 [lol]




General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Code:
 Benchmark Results - Sorted By Average Time 
Method Name Average Low High 
Method #2 Do While Loop 0.162 0.139 0.207 
Method #1 For NExt Loop 5.762 5.566 5.908 


Benchmark Results - UnSorted 
Method Name Average Low High 
Method #1 For NExt Loop 5.762 5.566 5.908 
Method #2 Do While Loop 0.162 0.139 0.207 



Full Benchmark Results - Sorted By Average Time 
Method Name Average Low High Run 1 Run 2 Run 3 Run 4 
Method #2 Do While Loop 0.162 0.139 0.207 0.160 0.143 0.207 0.139 
Method #1 For NExt Loop 5.762 5.566 5.908 5.566 5.727 5.908 5.848 

Times are averages from 4 runs of 10000 loops for each method.
Total Test Run Time: 29.86328s[/code

Interesting numbers are they not?

And Tarwn scripts always are great to use for these tests ;)


General FAQ   faq333-2924
5 steps to asking a question   faq333-3811
 
sorry about the "]" only 2 cups of coffee so far


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
:) Thanks

I didn't want the scope to go too far out but it is an important topic once everyone starting going toward readability, maintainability and of course efficiency comes down to the almighty line


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Wow, never thought that there could be that much of a difference between a for and while loop. What were you comparing?

I wonder how much (if at all) extra performance you would get out of GetRows() with a differant looping mechanism.

barcode_1.gif
 
I compared what was posted basically replacing the For Each with a Do While. Not a great test but the iterations themselves is all I was thinking about
Code:
Function Method1()
For i = 0 to 100
  For j = 0 to 5
    If i+j > 0 then

    End If
  Next
Next
End Function



Function Method2()
Do While i <= 100
  Do While j <= 5
    If i+j > 0 then

    End If
  Loop
Loop
End Function




General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top