while loop - multiple condition

Michael Torrie torriem at gmail.com
Tue Oct 14 00:08:36 CEST 2014

On 10/13/2014 11:12 AM, Rustom Mody wrote:
> On Monday, October 13, 2014 10:13:20 PM UTC+5:30, Rob Gaddi wrote:
>> On Mon, 13 Oct 2014 09:26:57 -0700 (PDT)
>> Rustom Mody  wrote:
>>> On Monday, October 13, 2014 9:43:03 PM UTC+5:30, Rob Gaddi wrote:
>>>> On Mon, 13 Oct 2014 09:56:02 +1100
>>>> Steven D'Aprano  wrote:
>>>>> When you have multiple clauses in the condition, it's easier to reason about
>>>>> them if you write the clauses as positive statements rather than negative
>>>>> statements, that is, "something is true" rather than "something is not
>>>>> true", and then use `not` to reverse it if you want to loop *until* the
>>>>> overall condition is true.
>>>> I was just explaining this concept to a young pup the other day.  De
>>>> Morgan's lets you say that (not (p and q)) == ((not p) or (not q)), but
>>>> the positive logic flavor is substantially less error-prone.  People
>>>> are fundamentally not as good at thinking about inverted logic.
>>> Curious: Which of
>>> - (not (p and q))
>>> - ((not p) or (not q))
>>> is more positive (less negative)??
>> The first is asking you to compare positive conditions (p and q) and
>> negate the entire thing (NAND).  The second asks you to think about
>> the combination of two different "not true" pieces of logic
>> (OR of two inverted inputs).  The first is pretty straightforward, and
>> I usually see people get it right.  The second gets screwed up as often
>> as not.
>> And of course, any combination of ands and ors should be broken into
>> multiple statements with a descriptive variable name in the middle or
>> all hope is lost.
> Yeah I guess 2 nots is one more than one!
> However (to my eyes)
> while i < N  and  a[i] != X:
> looks less negative than
> while not (i==N or a[i] == X):
> [Of course i < N is not identical to i != N ]

Right it should have been not (i >= N or a[i] == X) to be equivalent.

In assembler it's often best to reverse the condition and then use the
opposite jump mnemonic. IE, if the test is to see if a number not zero,
use the jump if zero command instead.  Often it reduces the number of
jumps required and eliminates the need to jump over the body of the "if"

if a != 0 then jump to bigger
jump to end


if a == 0 then jump to end

More information about the Python-list mailing list