Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Loops: "FOR index = 1 TO 0"

Author
Message
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 12th Aug 2016 11:56 Edited at: 12th Aug 2016 11:59
I've noticed something by accident, if you do this the loop doesn't run at all:

Is this something I could rely on?
Is this intended behavior or is AppGameKit compensating for bad code?

Normally I would do this:

The author of this post has marked a post as an answer.

Go to answer

Preben
AGK Studio Developer
20
Years of Service
User Offline
Joined: 30th Jun 2004
Location:
Posted: 12th Aug 2016 12:36 Edited at: 12th Aug 2016 13:00
if something < 1 then use "step -1" to make it loop, otherwise if something < 1 if will always break out, so you could rely on that.
best regards Preben Eriksen,
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 12th Aug 2016 13:05
I believe the C++ equivalent to the FOR loop is <=. So it basically checks if your condition is less than or equal too. Unless your step value is negative. As long as the result is true, the loop will run through.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Aug 2016 14:13 Edited at: 12th Aug 2016 15:33
the default step is 1
a to b must be same or b greater
step is optional
AGK (Steam) V2.0.20 : Windows 10 Pro 64 Bit : AMD (16.3.2) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 15th Aug 2016 08:41
It need to look like this:



As Markus says, normally it will go in steps of +1, the for loop ALWAYS counts 1 up unless you type STEP -1, or some other value after the variables. You could also type STEP 3 if you want it go 3 steps up every time it loops, or STEP -10, or whatever
13/0
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Aug 2016 11:35
This post has been marked by the post author as the answer.
It sounds like you aren't interested in counting backwards in your loop.

In English, the FOR statement is saying:

is Index greater than something yet? If no, process code. If yes, stop processing code.

So you have not broken any rules, and it is a perfectly valid scenario. Unusual, but valid.
This is also valid, and helps to explain your scenario. something can change during the lifetime of the loop...



...and will end after the third loop, because something has become less than i.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 15th Aug 2016 15:09
Interesting, thanks for all the replies! Who would have thought such a simple thing would get so much attention.

Quote: "It sounds like you aren't interested in counting backwards in your loop."

Correct, there are just times in my code where 'something' would equal zero.
Knowing this is normal behavior, i can ditch the "IF something : ... : ENDIF" and tidy up the code!
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 15th Aug 2016 16:36
If you don't know the step beforehand, you could do:

Pseudocode

Using AGKv2 Tier1

Login to post a reply

Server time is: 2024-09-29 17:25:39
Your offset time is: 2024-09-29 17:25:39