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

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

My whole point was to NOT process stuff sometimes, so that the counter results would be sequential, such as:
 1: apple
 2: carrot
 3: dapple
 4: fapple
 5: gourd

but apparently that can only be done by working with big IF statements and lots of indenting!
FUNCTION testNext()
   testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
   testInt = 0
   FOR EACH fruit IN testA
      IF TYPE(fruit) = "roString"
testInt++
? "testNext() found a String, fruit " fruit ", testInt" testInt
' And do 50 more lines of code,
' all indented to be within this huge IF block,
' instead of just skipping to next item in list,
' too bad
' Could maybe use GOTO but that's not something I like to do,
' So the IF block with indenting is the lesser of two evils!
END IF
   END FOR
END FUNCTION
0 Kudos
speechles
Roku Guru

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

FUNCTION testNext()
   testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
   index = 0
   while index < testA.count()
       fruit = testA[index]
       if type(fruit) = "string" then 'do stuff
       index++
   end while 'wend
END Function

The while/endwhile (while/wend) is probably a better choice. You can iterate the array at any moment you like forward _and_ backwards in any increment. For has a limitation of iteration forward only.

So you know apple has a numeric after it to tell the power of the apple when you eat how much it fills you up. Different apples fill up your hunger differently.

If type(fruit) = "string"
   if fruit = "apple"
       fill = testA[index+1]
       m.hunger -= fill ' eat the apple
       ' skip to next entry and skip value assigned to apple
       index +=2
       

Still not sure what you want to do but If I knew I bet I could figure out how to do what you want.. ^_~
0 Kudos
squirreltown
Roku Guru

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

FUNCTION testNext()

   testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
   testInt = 0
   FOR i = 0 to testA.count()-1
        IF TYPE( testA[i]) = "roString"
         testInt++
         ? "testNext() found a String, fruit " testA[i] ", testInt" testInt
        ELSE
        END IF
   END FOR
END FUNCTION



testNext() found a String, fruit apple, testInt 1
testNext() found a String, fruit carrot, testInt 2
testNext() found a String, fruit dapple, testInt 3
testNext() found a String, fruit fapple, testInt 4
testNext() found a String, fruit gourd, testInt 5
Kinetics Screensavers
0 Kudos
Komag
Roku Guru

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

"speechles" wrote:
Still not sure what you want to do but If I knew I bet I could figure out how to do what you want.. ^_~

I literally just wanted to avoid indenting a lot of lines in a big IF block.

If I could just disqualify an Item on the list and say "Next" and have it skip the rest of the FOR loop code for this iteration, then the rest of the FOR loop wouldn't have to be written within an indented IF block, but rather just be lines that are not indented.

Thank you for the WHILE loop example, and I like how it gives more manual control, but the same issue still applies, need to use an indented IF block.
0 Kudos
belltown
Roku Guru

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

In general, putting large blocks of code inside nested IF statements (or even using multiple nested IF statements) is considered poor programming practice, so you're on the right track. However, you can easily avoid this by separating out the fruit-processing code into its own function:


Function doFruit(num As Integer, fruit As String)
   ' Do all the fruit processing here ... '
   ? num; ": "; fruit
End Function

Sub Main()
  testA = ["apple", 2, "carrot", "dapple", 5, "fapple", "gourd"]
  num = 0
  For Each fruit In testA
     If Type(fruit) = "roString" Then doFruit(num, fruit) : num++
  End For
End Sub



0: apple
1: carrot
2: dapple
3: fapple
4: gourd

EDIT: Put the num++ statement before or after the doFruit() function call depending on whether you want the fruit list to start at 1 or zero.
0 Kudos
Komag
Roku Guru

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

Yeah, I think that's probably the best/cleanest approach.
0 Kudos