While versus Repeat


Just like in programming, Repeat can be considered a generalization of While (K&R: the for loop is a generalization of the while).

The difference between While with Counter and Repeat with Counter is that when you use While with Counter, the counter doesn’t necessarily control the number of loop iterations. Typically you would use some boolean condition (for example, number of polygons reduced by more than 50%) to control the number of loops, rather than an incremented counter. The counter is a convenience, there for you to use inside the while loop.

Here’s a rather artificial example of equivalent While and Repeat loops. The While loop uses a counter inside the loop, and also in the condition. Note that I had to subtract one for the While loop, because inside While with Counter, the counter is incremented after the loop is executed.

In this example, it would be simpler to just use the Max Repeats:

One thought on “While versus Repeat

  1. It would be interesting to know, which one is faster in your second setup: the “Repeat with Counter” or the “While with Counter” with the Max Repeat?
    😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s