Python IF THEN chain equivalence

Ethan Furman ethan at stoneleaf.us
Fri Nov 14 01:03:23 EST 2008


Grant Edwards wrote:
> On 2008-11-14, Ethan Furman <ethan at stoneleaf.us> wrote:
>> jzakiya wrote:
>>> I'm translating a program in Python that has this IF Then chain
>>>
>>>
>>> IF  x1 < limit:   --- do a ---
>>>     IF  x2 < limit:  --- do b ---
>>>         IF x3 < limit:  --- do c ---
>>>                        .-----
>>>                         ------
>>>                     IF  x10 < limt: --- do j ---
>>>                     THEN
>>>                  THEN
>>>               -----
>>>           THEN
>>>      THEN
>>> THEN
>>>
>>> In other words, as long as    'xi' is less than 'limit' keep going
>>> down the chain, and when 'xi' isn't less than 'limit' jump to end of
>>> chain a continue.
>> if x1 < limit:
>>      do a
>> if x2 < limit:
>>      do b
>> if x3 < limit:
>>      do c
>>    .
>>    .
>>    .
>>   etc
> 
> That doesn't do what the OP specified.  If any of the
> conditions fail, it should "jump" to the end and not execute
> _any_ further "do" statements regardless of the values of
> subsequent conditions.

Ack -- I was aware of the lack of jump, but missed that x7 might be less 
than limit even if x6 is greater -- thanks.

>> On the plus side, it's easy to read and understand -- on the
>> minus side, it doesn't jump to the end once the tests start
>> failing.
> 
> If all you want is easy to read and understand, then this is
> event simpler:
> 
> 
> sys.exit(0)
> 
<sheepish grin>
~ethan~



More information about the Python-list mailing list