Search code examples
loopscontinuepovray

Simulating the "continue" directive, and other alternatives


Some programming languages support a "continue" directive in loops to skip to the next loop. How do you achieve the same thing in languages that don't have this directive? I am facing a situation now in povray where I need this behavior. Thanks.


Solution

  • In the simplest case, just use a conditional:

    #for (i, from, to)
      // do common actions
      #if (<condition is true>)
        // do something special to the condition
      #end
    #end
    

    Alternatively, POV-Ray 3.7 supports the #break statement that you can use to emulate continue with a little help from recursion - however, this is quite clumsy and inelegant:

    #macro LoopWithContinuation(from, to)
      #for (i, from, to)
        #if (<condition is true>)
          LoopWithContinuation(i + 1, to)
          #break
        #else
          // do something
        #end
        #debug ""
      #end
    #end
    
    LoopWithContinuation(1, 20)
    

    Keep in mind, though, that POV-Ray has a fixed recursion depth limit of 200, so this approach is not suitable for longer loops.