Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Komag
Roku Guru

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?
0 Kudos
15 REPLIES 15
destruk
Streaming Star

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
0 Kudos
speechles
Roku Guru

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.
0 Kudos
Komag
Roku Guru

Re: How to skip an iteration in a FOR loop?

destruk, that gives me:

Syntax Error. (compile error &h02) in pkg:/source/mn.brs(1846)

which is the line with Next
0 Kudos
Komag
Roku Guru

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.
0 Kudos
destruk
Streaming Star

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
0 Kudos
squirreltown
Roku Guru

Re: How to skip an iteration in a FOR loop?

NEXT does not work for me  inside that if statement. Syntax error.
Kinetics Screensavers
0 Kudos
destruk
Streaming Star

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
0 Kudos
destruk
Streaming Star

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.
0 Kudos
belltown
Roku Guru

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
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.