Ray Y Member Apr 7, 2018 #1 Given: For I = 0 To somelist.Size - 1 Is the expression "somelist.Size - 1" recomputed every loop iteration?
Given: For I = 0 To somelist.Size - 1 Is the expression "somelist.Size - 1" recomputed every loop iteration?
Ed Brown Active Member Licensed User Longtime User Apr 7, 2018 #2 It should be - yes. This is because the list 'somelist' can change in size while inside the loop. If the list does not change size then it is more efficient (with large lists) to set the another variable to the size of the list before the loop. Upvote 0
It should be - yes. This is because the list 'somelist' can change in size while inside the loop. If the list does not change size then it is more efficient (with large lists) to set the another variable to the size of the list before the loop.
KMatle Expert Licensed User Longtime User Apr 7, 2018 #3 Nope. Example: If you remove items from a list while processing you need to do it backwards: Code: For list.Size-1 To 0 Step -1 otherwiese you get an outofbounds exeption. Upvote 0
Nope. Example: If you remove items from a list while processing you need to do it backwards: Code: For list.Size-1 To 0 Step -1 otherwiese you get an outofbounds exeption.
Ed Brown Active Member Licensed User Longtime User Apr 7, 2018 #4 KMatle said: Nope. Click to expand... Hmm, you're right. But this is also not the way it's meant to be. Why is B4J optimising the for/loop by assigning a variable (as my original suggestion)? There are times where this is not the desired behaviour and for B4J to simply just do this is WRONG and not the normal behaviour. Upvote 0
KMatle said: Nope. Click to expand... Hmm, you're right. But this is also not the way it's meant to be. Why is B4J optimising the for/loop by assigning a variable (as my original suggestion)? There are times where this is not the desired behaviour and for B4J to simply just do this is WRONG and not the normal behaviour.
KMatle Expert Licensed User Longtime User Apr 7, 2018 #5 Ed Brown said: B4J to simply just do this is WRONG and not the normal behaviour Click to expand... In VB.net it's the same. The FOR statement is initialized ONCE with the given values. Changing it later will not cause the FOR loop to finish (e.g. if it was 10 at start and now it's 5 it will not change the terminator of the FOR statement). Upvote 0
Ed Brown said: B4J to simply just do this is WRONG and not the normal behaviour Click to expand... In VB.net it's the same. The FOR statement is initialized ONCE with the given values. Changing it later will not cause the FOR loop to finish (e.g. if it was 10 at start and now it's 5 it will not change the terminator of the FOR statement).
Ray Y Member Apr 7, 2018 #6 Programming 101: Don't use a FOR loop unless you know the number of times you want the loop executed. And certainly don't change the loop variables inside the loop. I scream every time I see: Code: x = 10 For i = 1 to x do something if WeAreDone then x = 1 next i Lazy lazy lazy. This is stuff C programmers do all the time. Drives me crazy. Plus, it makes debugging big loops a nightmare. This is what WHILE is for. Anyway, I was just asking to know if I had to use a temp variable to store the computed result, or if it works the way _I_ think it should and optimizes it. And I was too lazy to write my own test. Thanks all. Upvote 0
Programming 101: Don't use a FOR loop unless you know the number of times you want the loop executed. And certainly don't change the loop variables inside the loop. I scream every time I see: Code: x = 10 For i = 1 to x do something if WeAreDone then x = 1 next i Lazy lazy lazy. This is stuff C programmers do all the time. Drives me crazy. Plus, it makes debugging big loops a nightmare. This is what WHILE is for. Anyway, I was just asking to know if I had to use a temp variable to store the computed result, or if it works the way _I_ think it should and optimizes it. And I was too lazy to write my own test. Thanks all.
Erel B4X founder Staff member Licensed User Longtime User Apr 8, 2018 #7 Ed Brown said: But this is also not the way it's meant to be. Why is B4J optimising the for/loop by assigning a variable (as my original suggestion)? Click to expand... There is no right or wrong here. This is how For loops behave in B4X. https://www.b4x.com/android/forum/pages/results/?query=for Upvote 0
Ed Brown said: But this is also not the way it's meant to be. Why is B4J optimising the for/loop by assigning a variable (as my original suggestion)? Click to expand... There is no right or wrong here. This is how For loops behave in B4X. https://www.b4x.com/android/forum/pages/results/?query=for
KMatle Expert Licensed User Longtime User Apr 8, 2018 #8 From the documentation: Note that the loop limits will only be calculated once before the first iteration. Click to expand... Upvote 0
From the documentation: Note that the loop limits will only be calculated once before the first iteration. Click to expand...
Ray Y Member Apr 8, 2018 #9 KMatle said: From the documentation: Click to expand... Ah...RTFM. My apologies for not seeing that. Where is that phrase actually located (i.e. which "documentation"?) Upvote 0
KMatle said: From the documentation: Click to expand... Ah...RTFM. My apologies for not seeing that. Where is that phrase actually located (i.e. which "documentation"?)
Erel B4X founder Staff member Licensed User Longtime User Apr 8, 2018 #10 See the description before the search results: https://www.b4x.com/android/forum/pages/results/?query=for Upvote 0
See the description before the search results: https://www.b4x.com/android/forum/pages/results/?query=for
Ray Y Member Apr 8, 2018 #11 Erel said: See the description before the search results: https://www.b4x.com/android/forum/pages/results/?query=for Click to expand... Yes, I see it there, but where did that description come from? Not a forum post. Wherever it is, I obviously need to take a look at the rest of what's there to avoid posting another RTFM question in the future. Upvote 0
Erel said: See the description before the search results: https://www.b4x.com/android/forum/pages/results/?query=for Click to expand... Yes, I see it there, but where did that description come from? Not a forum post. Wherever it is, I obviously need to take a look at the rest of what's there to avoid posting another RTFM question in the future.
Erel B4X founder Staff member Licensed User Longtime User Apr 8, 2018 #12 Keywords documentation: https://www.b4x.com/android/help/core.html#keywords You can also see it in the IDE itself: Upvote 0
Keywords documentation: https://www.b4x.com/android/help/core.html#keywords You can also see it in the IDE itself:
Ray Y Member Apr 8, 2018 #13 Much thanks. Is the IDE stuff a duplicate of that Keywords core help documentation? Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Apr 8, 2018 #14 They both extract the information from the libraries XML files. There are also several "help viewers" tools that will show you the same information from your list of libraries. Upvote 0
They both extract the information from the libraries XML files. There are also several "help viewers" tools that will show you the same information from your list of libraries.