|
Post by johnno56 on Mar 16, 2020 3:57:45 GMT -6
I'm not 100% sure what it is you are checking...
If I am looking through a list and I don't find it. I just exit the loop.
For i = 1 to somevar if somevar <> whatever then exit for end if next
I may have made your solution all the more difficult. If so I apologuise.
J
|
|
|
Post by johnno56 on Mar 16, 2020 6:49:13 GMT -6
Hmm... The first time I read your code snippets, I assumed a typo and didn't give it any mind, until you repeated the same line. You may have made a typo, so forgive me if I am being too 'picky'... "for I = 1 to 0 step 1" seems to be incorrect... I believe it should read "for I = 1 to 0 step - 1". I may not be correct but, you are wanting to count 'down' from 1 to zero whilst trying to step 'up' by positive one. Just a thought...
|
|
|
Post by n00b on Mar 16, 2020 8:58:50 GMT -6
Johnno is correct. If you want to loop from high to low then set your step to -1.
|
|
|
Post by rosy on Feb 10, 2021 17:09:22 GMT -6
There are situations where such a loop is justified. It shouldn't be executed, and in RC Basic it works forever ...
|
|
|
Post by n00b on Feb 10, 2021 20:17:03 GMT -6
Internally RCBasic sets either a positive or negative flag on each for loop when it starts. Its based on whether the starting value for a variable is greater than or less than the end condition. If it is positive then the end condition will be when the counter is greater than or equal to the end condition. Otherwise it will check if the counter is less than the end condition. To avoid an infinite loop just have an if statement to check if a for loop should execute.
|
|
|
Post by kennn on Feb 11, 2021 4:25:35 GMT -6
|
|
|
Post by rosy on Feb 11, 2021 6:59:46 GMT -6
If it were like that, you wouldn't have to add IF ...
|
|
|
Post by n00b on Feb 12, 2021 10:37:22 GMT -6
If it were like that, you wouldn't have to add IF ... FOR loops in RCBasic have a default step of 1 unless otherwise specified. They are used for iterating through data or counting. If you need a conditional loop, I would recommend using a WHILE loop.
|
|
|
Post by rosy on Feb 12, 2021 12:47:07 GMT -6
I don't think we understand each other ...
We have For I = 1 to X. You said FOR checks if I> = X and stops if so. So if X = 0 then it should break, not last forever.
|
|
|
Post by n00b on Feb 12, 2021 13:55:27 GMT -6
I don't think we understand each other ... We have For I = 1 to X. You said FOR checks if I> = X and stops if so. So if X = 0 then it should break, not last forever. Sorry I was mistaken. Its based on whether STEP is positive or negative. Sorry for the confusion.
|
|
|
Post by rosy on Feb 12, 2021 15:16:11 GMT -6
But when it is positive, it should do as I wrote, and not do so ...
|
|
|
Post by n00b on Feb 13, 2021 13:43:26 GMT -6
I just checked the code and I was actually right the first time. It does depend of whether or not the start condition is greater than or less than the end condition.
Here is the C++ code that does that comparison:
if(loop_stack.top().isNegative && (loop_stack.top().counter[0].nid_value[0].value[0] + loop_stack.top().f_step) >= loop_stack.top().f_end) { loop_stack.top().counter[0].nid_value[0].value[0] += loop_stack.top().f_step; current_address = f_addr; } else if( (!loop_stack.top().isNegative) && (loop_stack.top().counter[0].nid_value[0].value[0] + loop_stack.top().f_step) <= loop_stack.top().f_end) { loop_stack.top().counter[0].nid_value[0].value[0] += loop_stack.top().f_step; //cout << "current counter = " << loop_stack.top().counter[0].value[0] << " step = " << loop_stack.top().f_step << endl; current_address = f_addr; } else { loop_stack.pop(); //current_loop_stack_count--; }
As for why I chose to do it this way, it was for speed. But I will probably make this change anyway as the speed increase from doing it this way was only about 15ms to 20ms in a test loop of 50 million. I can probably get this change into the next release.
|
|
|
Post by rosy on Feb 13, 2021 15:10:09 GMT -6
I don't really understand what these variables and functions mean, but it looks strange ...
|
|
|
Post by rosy on Feb 14, 2021 6:59:34 GMT -6
Can you explain it?
|
|
|
Post by n00b on Feb 14, 2021 14:39:51 GMT -6
I already made the changes to the FOR loop code you suggested so this is code is no longer used.
But if you want to just understand what its doing, basically it checks whether the loop is positve or negative and also checks if the counter plus the step has passed the end condition. If it hasn't then it sets the current address that code is being executed from back to the start of the FOR loop.
|
|