For...Next loop with Arrays

New to FreeBASIC? Post your questions here.
toml_12953
Posts: 27
Joined: Jul 07, 2005 12:37
Location: Malone, NY
Contact:

FOR...NEXT variable use

Postby toml_12953 » Feb 07, 2008 18:42

There's a good reason for being able to use the value of the index variable outside the loop:


FOR i=1 TO 10
IF a(i) = TestVal THEN EXIT FOR
NEXT i
IF i< 11 THEN ' We found a match

This saves having to use a flag.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Feb 07, 2008 18:53

If you wanted to do that with arrays using a flag would still be more efficient, and therefore better.
counting_pine
Site Admin
Posts: 6219
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Feb 07, 2008 19:25

I'm not sure I agree with you about efficiency. Having a flag introduces another variable to the system, that has to be set and checked.
Additionally, I'd say there's a good chance that, if you check whether the counter is within bounds immediately after the For loop, then an optimising compiler can remove it, since such a check was needed for it to exit the loop in the first place.

I'd say that, if you're using integer variables, a step of 1, and the counter isn't modified within the loop, then its exit value is well-defined. Some people don't trust it, but others do.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Feb 07, 2008 22:49

counting_pine wrote:I'm not sure I agree with you about efficiency. Having a flag introduces another variable to the system, that has to be set and checked.

But once again, if you're using an array you're going to have an automatic slow-down as the array index has to be calculated by adding it to the first element of the array.

Additionally, I'd say there's a good chance that, if you check whether the counter is within bounds immediately after the For loop, then an optimising compiler can remove it, since such a check was needed for it to exit the loop in the first place.

Yes, but at this point the compiler doesn't optimize, and that kind of thing would be hard to check (once the compiler does optimize).

I'd say that, if you're using integer variables, a step of 1, and the counter isn't modified within the loop, then its exit value is well-defined. Some people don't trust it, but others do.

I don't say you should never use the iterator outside the loop, I just say be careful because technically the value is undefined and other than the example given I can't think of a reason to do it. Given that the people who might be most likely to do it wouldn't necessarily know what the value is (they might think it's n when in fact it's n+1), it would be wise to be cautious. And yeah, if you ever modify the counter within the loop, use non-integer variables, or use a different step value than 1, it could be different - which is why, once again, people need to be cautious not to assume anything unless they test it out first.

But once again, the main issue here seems to be allowing array items or UDT members to be iterators, and that's obviously going to hit your performance because of the extra add that will occur at least once if not more.

Return to “Beginners”

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests