for to avoid executing when listitems = 0?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

for to avoid executing when listitems = 0?

Post by Bugala »

I just got a minor problem and I am trying to figure out best way to get around this problem.

In short idea is following:

Code: Select all

for n = 0 to listitems(table)-1
                do stuff with table[n] item
next 
works fine, unless there happens to be zero items in table.

I am now wondering best way to avoid this issue, so far i have thought of two possibilities:

Code: Select all

if listitems(table) <> 0 
for n = 0 to...
endif
or

Code: Select all

n = 0 to...
if n<>-1
do stuff
endif
These both work, but i would like some better idea, preferably something where i wouldnt need to add any extra code.
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: for to avoid executing when listitems = 0?

Post by peceha »

if you have something like this:

Code: Select all

for n = 0 to listitems(table)-1
and listitems(table)=0 then this FOR/NEXT loop will never be executed since listitems(table)-1=-1 (it would be executed if you specified STEP -1)

So you don't need any extra code .
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: for to avoid executing when listitems = 0?

Post by Bugala »

Ah, thanks. That was very helpful.

Seems i misremembered how for works.

I thought if result had been "0 to -1" then it would first execute 0 then -1, but it actually skips it entirely unelss you use the "STEP". Good.

Thanks for help. Problem solved.
Post Reply