
Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2019
07:47 PM
How to skip an iteration in a FOR loop?
FUNCTION testNext()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
testInt = 0
FOR EACH fruit IN testA
testInt++
IF TYPE(fruit) = "roString" THEN ? fruit ELSE testA.Next()
? "testNext() did one step, fruit " fruit ", testInt" testInt
END FOR
END FUNCTION
This doesn't do what I want, it increments the counter but doesn't skip the rest of the loop cycle, and then the counter is incremented AGAIN at the bottom, so "carrot" never prints out, etc. I'd like to avoid using GOTO if possible. Any ideas?
15 REPLIES 15
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019
08:26 AM
Re: How to skip an iteration in a FOR loop?
FUNCTION testNext()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
testInt = 0
FOR EACH fruit IN testA
testInt++
IF TYPE(fruit) = "roString" THEN
? fruit
ELSE
Next
END IF
? "testNext() did one step, fruit " fruit ", testInt" testInt
END FOR
END FUNCTION

speechles
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019
10:21 AM
Re: How to skip an iteration in a FOR loop?
What exactly are you trying to do? Why are you increment in the for each loop?
FUNCTION testNext()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
for fruit = 0 to testA.count()-1
if type(testA[fruit]) = "roString" ? testA[fruit]
? "testNext(): did one step; fruit="+testA[fruit].toStr()+" index="+fruit.toStr()
next fruit
END Function
Wouldn't that be easier.. I mean sorta? Are you just trying to build a string of names of fruit from among a non sorted one dimensional array? What exactly are you trying to do sir.. lol
Is shorter better or does adding that extra reference to the array mean it slows down the code and longer is better. You be the judge. you need a huge array to test the millisecond differences. In a list small as this the difference is NIL.
----------------------------------------------------------------
For some reason the Roku docs do not note the special use of "next varname" as an option.
Take this for example:
for fruit = 0 to testA.count()-1
for potion = 0 to testB.count()-1
for skill = 0 to testC.count()-1
Now at any time we can "Next fruit" or "Next potion" or "Next skill" and not involve the other nested for loops incrementing. Using "End for" will interact with the last nested for. Using "next varname" will increment the loop for the associated varname.
Why isn't this in the Roku documentation?
@RokuTannerD This is an oversight and should be corrected. You can use "Next varname" when using "For varname=" in a loop. When doing this it allows incrementing nested for's out of order which the "End for" does not allow. Thanks. ^_~
https://developer.roku.com/docs/references/brightscript/language/program-statements.md
it is that page that is incorrect --^
Also.. why can't we
For each {varname1 varname2 varname3} in var ? Other languages allow this but not brightscript.
FUNCTION testNext()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
for fruit = 0 to testA.count()-1
if type(testA[fruit]) = "roString" ? testA[fruit]
? "testNext(): did one step; fruit="+testA[fruit].toStr()+" index="+fruit.toStr()
next fruit
END Function
Wouldn't that be easier.. I mean sorta? Are you just trying to build a string of names of fruit from among a non sorted one dimensional array? What exactly are you trying to do sir.. lol
Is shorter better or does adding that extra reference to the array mean it slows down the code and longer is better. You be the judge. you need a huge array to test the millisecond differences. In a list small as this the difference is NIL.
----------------------------------------------------------------
For some reason the Roku docs do not note the special use of "next varname" as an option.
Take this for example:
for fruit = 0 to testA.count()-1
for potion = 0 to testB.count()-1
for skill = 0 to testC.count()-1
Now at any time we can "Next fruit" or "Next potion" or "Next skill" and not involve the other nested for loops incrementing. Using "End for" will interact with the last nested for. Using "next varname" will increment the loop for the associated varname.
Why isn't this in the Roku documentation?
@RokuTannerD This is an oversight and should be corrected. You can use "Next varname" when using "For varname=" in a loop. When doing this it allows incrementing nested for's out of order which the "End for" does not allow. Thanks. ^_~
https://developer.roku.com/docs/references/brightscript/language/program-statements.md
it is that page that is incorrect --^
Also.. why can't we
For each {varname1 varname2 varname3} in var ? Other languages allow this but not brightscript.

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019
06:48 PM
Re: How to skip an iteration in a FOR loop?
destruk, that gives me:
which is the line with Next
Syntax Error. (compile error &h02) in pkg:/source/mn.brs(1846)
which is the line with Next

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019
07:09 PM
Re: How to skip an iteration in a FOR loop?
speechles, that's very interesting about using NEXT Var in place of END FOR.
(The integer increment was just for information on the printout to help me clarify what was going on.)
Basically I'm trying to process objects in an array, and if the object has a certain property, do all the rest of the processing, otherwise I want to skip to the next object. My current solution is to simply make it a huge IF block, indenting over dozens of lines of code, which aesthetically I'd rather not do but it works just fine. So really this thread is more academic and not really important.
(The integer increment was just for information on the printout to help me clarify what was going on.)
Basically I'm trying to process objects in an array, and if the object has a certain property, do all the rest of the processing, otherwise I want to skip to the next object. My current solution is to simply make it a huge IF block, indenting over dozens of lines of code, which aesthetically I'd rather not do but it works just fine. So really this thread is more academic and not really important.
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2019
08:31 AM
Re: How to skip an iteration in a FOR loop?
"Komag" wrote:
destruk, that gives me:Syntax Error. (compile error &h02) in pkg:/source/mn.brs(1846)
which is the line with Next
That is odd. Perhaps they can not be mixed, but all of my loops I write for my own apps use Next instead of End For.
So I'll need to test and see if replacing End For here with Next resolves that.
Next is a reserved word in brightscript and I was always under the impression they (End For and Next) are interchangeable.
FUNCTION testNext()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
testInt = 0
FOR EACH fruit IN testA
testInt++
IF TYPE(fruit) = "roString" THEN
? fruit
ELSE
Next
END IF
? "testNext() did one step, fruit " fruit ", testInt" testInt
Next
END FUNCTION

squirreltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2019
09:31 AM
Re: How to skip an iteration in a FOR loop?
NEXT does not work for me inside that if statement. Syntax error.
Kinetics Screensavers
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019
09:04 AM
Re: How to skip an iteration in a FOR loop?
"squirreltown" wrote:
NEXT does not work for me inside that if statement. Syntax error.
Weird, ok, you are right. It can't be within the IF statement.
But this works fine if you rewrite the loop slightly. And if you're wanting the index value of the array for the printed results, you should increment the value after the print statement.
Sub Main()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
testInt = 0
FOR EACH fruit IN testA
IF TYPE(fruit) = "roString" THEN
? fruit
? "testNext() did one step, fruit " fruit ", testInt" testInt
END IF
testInt++
Next
End Sub
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019
09:06 AM
Re: How to skip an iteration in a FOR loop?
Just for kicks I tried, and yes, End For can't be within the If statement either, so it's consistent. Other languages (like PHP) allow this - next will exit any subsequent nested if statements and increment the internal loop counter and re-execute the inner-most loop, just not brightscript.
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019
10:44 AM
Re: How to skip an iteration in a FOR loop?
Why not just let the loop construct handle the counter:
Sub Main()
testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
FOR i = 0 TO testA.Count() - 1
fruit = testA[i]
IF TYPE(fruit) = "roString" THEN
? i; ": "; fruit
END IF
END FOR
End Sub
0: apple
2: carrot
3: dapple
5: fapple
6: gourd