StoopKid8978 Member Jun 2, 2020 #1 I was wondering about iterations in lists. I read that "for each" is better than "for i", but I can't seem to grasp it completely. I a, specifically looking for a way to go through a list and get an item that is the first item on the list that is greater than a given value.. For example... Code: Copy Dim x as int = 12 Dim mylist As List mylist.Initialize mylist.AddAll(Array As Int(5, 10, 15, 20, 25)) if x > mylist.get(i) then "stop iterating and return that first value" Or maybe it would be better to get the ceil that's less than x? I am hoping this is explained well enough. I want it to ultimately subtract 10 from 12 and give the remainder maybe using mod Last edited: Jun 3, 2020
I was wondering about iterations in lists. I read that "for each" is better than "for i", but I can't seem to grasp it completely. I a, specifically looking for a way to go through a list and get an item that is the first item on the list that is greater than a given value.. For example... Code: Copy Dim x as int = 12 Dim mylist As List mylist.Initialize mylist.AddAll(Array As Int(5, 10, 15, 20, 25)) if x > mylist.get(i) then "stop iterating and return that first value" Or maybe it would be better to get the ceil that's less than x? I am hoping this is explained well enough. I want it to ultimately subtract 10 from 12 and give the remainder maybe using mod
Erel B4X founder Staff member Licensed User Longtime User Jun 3, 2020 #2 For Each is not exactly better than the regular For loop. Use the one that is easier for you. Code: Copy Sub FindFirstGreaterValue (List As List, Value As Int) As Int For Each v As Int In List If v > Value Then Return v Next Return -1 End Sub Code: Copy Dim i As Int = FindFirstGreaterValue(Array As Int(5, 10, 15, 20, 25), 12) Log(i) Upvote 0
For Each is not exactly better than the regular For loop. Use the one that is easier for you. Code: Copy Sub FindFirstGreaterValue (List As List, Value As Int) As Int For Each v As Int In List If v > Value Then Return v Next Return -1 End Sub Code: Copy Dim i As Int = FindFirstGreaterValue(Array As Int(5, 10, 15, 20, 25), 12) Log(i)
StoopKid8978 Member Jun 16, 2020 #4 i'm going to write a separate question about this in a different thread, but i didn't know if you could maybe explain a little further...If I wanted it to do the same thing if x = i how would that look? Would i different subs if I wanted the both to happen within my app? or could they be combined into the same sub? I guess a better question would be where can I find the documentation that explain what exactly each part is in your solution so I could implement as I see fit? Upvote 0
i'm going to write a separate question about this in a different thread, but i didn't know if you could maybe explain a little further...If I wanted it to do the same thing if x = i how would that look? Would i different subs if I wanted the both to happen within my app? or could they be combined into the same sub? I guess a better question would be where can I find the documentation that explain what exactly each part is in your solution so I could implement as I see fit?
Erel B4X founder Staff member Licensed User Longtime User Jun 16, 2020 #5 StoopKid8978 said: I guess a better question would be where can I find the documentation that explain what exactly each part is in your solution so I could implement as I see fit? Click to expand... Start with the guides or the video tutorials: https://www.b4x.com/learn.html There is nothing special in this code. Upvote 0
StoopKid8978 said: I guess a better question would be where can I find the documentation that explain what exactly each part is in your solution so I could implement as I see fit? Click to expand... Start with the guides or the video tutorials: https://www.b4x.com/learn.html There is nothing special in this code.