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.

DarkBASIC Professional Discussion / Conditional FOR NEXT loop doesn't work, but hey, I tried...

Author
Message
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 30th Jun 2012 05:04
First off, I came to a point where I thought I'd test out this:



And it doesn't work... But I had to try it our anyway for the fun of it.

Instead you have to go the long way round:



I've not yet tried it but I'm expecting it also not to work with REPEAT UNTIL, WHILE ENDWHILE, etc

It was fun just to try something out that to look at makes sense.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 30th Jun 2012 05:33 Edited at: 30th Jun 2012 05:34
Or:



Join or Support!
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 30th Jun 2012 06:59 Edited at: 30th Jun 2012 07:00
Quote: "I've not yet tried it but I'm expecting it also not to work with REPEAT UNTIL, WHILE ENDWHILE, etc
"


nah. loops aren't stack based so that's not going to work. In the FOR/NEXT case, the Next becomes either an absolute or PC relative branch back it's parent FOR statement.

Pincho Paxton
22
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 30th Jun 2012 13:57 Edited at: 30th Jun 2012 13:59
You get used to it. This is something that I approached 25 years ago.

Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 30th Jun 2012 23:04
Or:


Not as concise as Sasuke's but is dependant on logic rather than multiplication; so if Use is not zero, it will always count 10-20.

basjak
15
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 1st Jul 2012 01:52
Chris reminds me with c++. Never came to me to use it in dbpro but interesting idea to a rid of some ifs.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Jul 2012 02:08
Quote: "Not as concise as Sasuke's but is dependant on logic rather than multiplication"


What do you mean? You've still got the multiplications.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Jul 2012 11:27
And, if you need more speed...



TheComet

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 1st Jul 2012 13:35 Edited at: 1st Jul 2012 13:36
Originally I came across a need to use the IF to setup a loop, something I'd never done. Not necessarily to use a multiplication, but more like this: (it looked ok on paper)



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Indicium
17
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 1st Jul 2012 17:28
Quote: "What do you mean? You've still got the multiplications."


I think he means that the logic in the loop is more clear.


They see me coding, they hating. http://indi-indicium.blogspot.co.uk/
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 1st Jul 2012 17:35
The fastest method would be:



Join or Support!
Indicium
17
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 1st Jul 2012 17:41
Quote: "The fastest method would be"


That's inefficient, the code would have to be written out twice, once for each loop. So a change would have to be done twice.


They see me coding, they hating. http://indi-indicium.blogspot.co.uk/
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 1st Jul 2012 17:52
Quote: "That's inefficient, the code would have to be written out twice, once for each loop. So a change would have to be done twice."


True depending on the code, because if it's just calling a function/gosub then it's not that inefficient and still quicker than the previous methods that require multiplication. Annoyingly the compiler is slow, so best to use as least code as possible so a more ingenious method is in order.


Join or Support!
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 1st Jul 2012 21:41
Quote: "What do you mean? You've still got the multiplications."


@Green Gandalf

Now how am I supposed to know what I mean?

Seriously, I should have stated that I moved the multiplication out of the parenthesis, (use*10) was changed to (use<>0)*10, so if use=100, instead of outputting 220,230,240,250.... it outputs 10,11,12,13....

Should I post another method? Ok, I think I should post two...





Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Jul 2012 22:06
Quote: "(use*10) was changed to (use<>0)*10,"


Yes, I know. That was precisely my point: i.e. you've cunningly replaced one operation by two - and the upper limit of the for/next loop is recomputed each iteration I believe.
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 1st Jul 2012 22:08
Hey Chris, I guess we could just sign use before using it, or clamp it.


Join or Support!
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jul 2012 06:50
You're all doing it wrong, THIS is how PROS solve it!



TheComet

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2012 15:39
Quote: "THIS is how PROS solve it!"


Only those who work for MicroSoft?
GregA
14
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 2nd Jul 2012 19:30
Wow, I don't know if this thread is serious or not...

But write the code to be as clear as possible. Any amount of time that you save doing that will be greater than any amount of time that the compiler will ever spend compiling, or recalculating a few itinerant extra multiplications.

But for me, for something this tiny and small with only 20 lines of code at steak...

Just unroll the loop...



If someone wants to impress me, write a neural net and teach it how to count to 20
gamerboots
16
Years of Service
User Offline
Joined: 8th Dec 2008
Location: USA
Posted: 2nd Jul 2012 20:26
so take the conditional part out of the problem and you have your solution:



they will never know what bit them until its too late...
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2012 20:33
Unrolled loops might be OK for compiled code if speed really is that critical but for general use they just make the code long, verbose, and awkward to maintain. These things are largely a matter of taste and personal preference though - as long as you keep an eye on speed when it matters. In this case I prefer the compact versions suggested by Sasuke and TheComet for general use.

Quote: "Wow, I don't know if this thread is serious or not..."


Yes and no ... probably.
GregA
14
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 2nd Jul 2012 22:27
In the specific example cited, I would argue that my example is the more clear version of the code.

With something like DBPro, with all its creation commands, unrolling and using the code ~as~ data makes a lot of sense for a lot of applications.

For example... Lets say we were making a lot of cubes, that are non-arbitrarily sized and placed and we want to place them at specific locations, with specific texture maps, etc etc etc...



is much more readable than something like...



Never mind in that second case you would have all sorts of exceptions to your... parser... as you wanted to change things around. Like use a cone instead of a cube for example.
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2012 23:44
That's not a very convincing example.

Even when the first is corrected I still find the second far simpler to read and check.

As I said it's a matter of personal preference.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 3rd Jul 2012 00:01


I actually prefer your second example GregA.

If I was seriously considering a conditional loop, I would probably end up using:


Or the method suggested by Gamer Boots

Libervurto
19
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Jul 2012 00:58
If it's a huge loop it might help to do any calculations first:


Shh... you're pretty.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 4th Jul 2012 01:08
@Obese87 - That's originally what I was getting at, but only kind of... I couldn't use SELECT CASE because there were different values and different loops all over the place.

I needed to select a number or group of numbers from 0-255 and do various loops based on the value.

The Snippet from above somewhere:



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Jul 2012 19:00 Edited at: 4th Jul 2012 19:05
Well, since the for loop scope is evaluated each iteration, an approach might be to nest the condition inside the loop and change the scope parameters:



I used select, but if endif could've benn used.

Enjoy your day.
nonZero
14
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 4th Jul 2012 22:31
@Latch: Nice!!! Seriously! I had no idea you could change the actual condition variable in a FOR-loop. I always thought only the counter was evaluated once the loop was initiated. That information is very helpful to me as I like to optimize.


Anyways, glad to learn something new

@WLGfx: Okay, so Latch solved your problem but I've made this so I'll post it anyways. It's not really a clever solution as such but it'll give you good performance and it's universally useable in your code:


Well, yeah pretty much explained it in my comments (tried to make them a little humorous but my brilliant wit has taken a dip ever since I was born so I'm just trying to ride out this dip in my career as a comedian).
Anyhow, I dunno how useful this technique is but I've started making use of it a little recently (not in this context) and have found it makes life easy for me (but I'm weird among weird so I prolly don't count).

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 5th Jul 2012 06:06 Edited at: 5th Jul 2012 06:08
@nonZero - A lookup table! Genius man... I use them for all kinds and for a FOR NEXT loop I just didn't think of it. Durrr...

@Latch - In C/C++ you can alter variables inside the loop but generally it can be dangerous. It's something I'll probably have to test out in DBP and C again just to see what is safe. I doubt though you could change the condition though, but...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 5th Jul 2012 08:08
You can happily modify the for/next loop variable in most (if not all) variations of BASIC, but altering the end value is only supported in some forms today. So the compiler protects the loop start/end expression result prior to entering the construct.

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 5th Jul 2012 10:33 Edited at: 5th Jul 2012 10:36
Quote: "@Latch - In C/C++ you can alter variables inside the loop but generally it can be dangerous. It's something I'll probably have to test out in DBP and C again just to see what is safe. I doubt though you could change the condition though, but..."


I don't think it's dangerous to alter the loop control variable in C. A statement like



is perfectly valid. And the expression part of the loop is up for grabs - meaning it can be as complicated or simple or have nothing to do with the control value. For example:



Will keep looping until "q" is pressed on the keyboard.

Quote: "but altering the end value is only supported in some forms today"

I'm pretty sure in darkbasic the conditional part (what comes after TO) is evaluated each iteration.

Enjoy your day.
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2012 12:52
Quote: "I'm pretty sure in darkbasic the conditional part (what comes after TO) is evaluated each iteration."


It is. Try this.

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 6th Jul 2012 03:10
Quote: "I'm pretty sure in darkbasic the conditional part (what comes after TO) is evaluated each iteration."


I know, my point is that we can't rely upon such behaviors when shoveling code around. Some BASIC dialects still do, many don't.

Login to post a reply

Server time is: 2025-08-08 10:10:22
Your offset time is: 2025-08-08 10:10:22