for to avoid executing when listitems = 0?

Discuss any general programming issues here

for to avoid executing when listitems = 0?

Postby Bugala » Sat Sep 02, 2017 12:16 pm

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.
Bugala
 
Posts: 645
Joined: Sun Feb 14, 2010 8:11 pm

Re: for to avoid executing when listitems = 0?

Postby peceha » Sat Sep 02, 2017 12:48 pm

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 .
peceha
 
Posts: 110
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

Re: for to avoid executing when listitems = 0?

Postby Bugala » Sat Sep 02, 2017 1:22 pm

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.
Bugala
 
Posts: 645
Joined: Sun Feb 14, 2010 8:11 pm


Return to General programming

Who is online

Users browsing this forum: No registered users and 6 guests