multiple breaks

Robert Lehmann stargaming at gmail.com
Sat Nov 15 09:52:01 CET 2008


On Thu, 13 Nov 2008 02:16:32 -0800, Chris Rebert wrote:

> On Thu, Nov 13, 2008 at 2:07 AM, TP <Tribulations at paralleles.invalid>
> wrote:
>>
>> In the following example, is this possible to affect the two iterators
>> to escape the two loops once one "j" has been printed:
>>
> Non-exception alternative:
> 
> done = False
> for i in range(5):
>    for j in range(i):
>       print j
>         done = True
>         break
>     if done:
>         break

Another alternative is explicitly jumping in the outer loop::

  for i in range(5): # use xrange for larger ranges
      for j in range(i):
          print j
          break
      else:
          continue # applies already to the outer loop
      break

HTH,

-- 
Robert "Stargaming" Lehmann



More information about the Python-list mailing list