For Loop - For i = 5 to 1 NOT 1 to 5 (Decrease NOT Increase)

Cosmos75

Registered User.
Local time
Today, 09:03
Joined
Apr 22, 2002
Messages
1,280
Is there a way to get a For loop to decrease by 1 and not increase by 1?

Where RCount = 5, I want

For i = RCount to 1
'do something with i
Next i


I don't want

For i = RCount to 1
do something with i
Next i


Is this possible? I've tried it and it doesn't work.

Or will I have to do this (only way I could figure it out)

For i = 1 to RCount
h = RCount - i + 1
'do something with h
Next i
 
Cosmos, as a further piece of info, the For...Next... loop structure isn't just limited to increments or decrements of 1.

You can change the number that each loop increments or decrements with the Step keyword.

Code:
For i = 5 To 25 Step 5
    MsgBox i
Next i

This loop will increase the value of i by 5 on every turn, taking 5 turns to do so.

Code:
For i = 25 To 5 Step -5
    MsgBox i
Next i

This loop, obviously, will decrese the value of i by 5 on every turn, taking 5 turns to do so.
 
THANKS!

Just posting back to thank everyone who replied!
 

Users who are viewing this thread

Back
Top Bottom