Simulating C-style for loops in python
<p>(even the title of this is going to cause flames, I realize)</p>

<p>Python made the deliberate design choice to have the <code>for</code> loop use explicit iterables, with the benefit of considerably simplified code in most cases.  </p>

<p>However, sometimes it is quite a pain to construct an iterable if your test case and update function are complicated, and so I find myself writing the following while loops:</p>

<pre><code>val = START_VAL
while &lt;awkward/complicated test case&gt;:
    # do stuff
    ...
    val = &lt;awkward/complicated update&gt;
</code></pre>

<p>The problem with this is that the update is at the bottom of the <code>while</code> block, meaning that if I want to have a <code>continue</code> embedded somewhere in it I have to:</p>

<ul>
<li><p>use duplicate code for the complicated/awkard update, AND </p></li>
<li><p>run the risk of forgetting it and having my code infinite loop</p></li>
</ul>

<p>I could go the route of hand-rolling a complicated iterator:</p>

<pre><code>def complicated_iterator(val):
    while &lt;awkward/complicated test case&gt;:
         yeild val
         val = &lt;awkward/complicated update&gt;

for val in complicated_iterator(start_val):
    if &lt;random check&gt;:
         continue # no issues here
    # do stuff
</code></pre>

<p>This strikes me as waaaaay too verbose and complicated.  Do folks in stack overflow have a simpler suggestion?</p>

<p>Response to comments:</p>

<p>@Glenn Maynard: Yes, I dismissed the answer. It's bad to write five lines if there is a way to do it in one... especially in a case that comes up all the time (looping being a common feature of Turing-complete programs). </p>

<p>For the folks looking for a concrete example: let's say I'm working with a custom date library. My question would then be, how would you express this in python: </p>

<pre><code>for (date = start; date &lt; end; date = calendar.next_quarter_end(date)):
    if another_calendar.is_holiday(date):
       continue
    # ... do stuff...
</code></pre>
