Short-circuit Logic

rusi rustompmody at gmail.com
Thu May 30 12:58:44 EDT 2013


On May 30, 5:58 pm, Chris Angelico <ros... at gmail.com> wrote:
> The alternative would be an infinite number of iterations, which is far far worse.

There was one heavyweight among programming teachers -- E.W. Dijkstra
-- who had some rather extreme views on this.

He taught that when writing a loop of the form

i = 0
while i < n:
  some code
  i += 1

one should write the loop test as i != n rather than i < n, precisely
because if i got erroneously initialized to some value greater than n,
(and thereby broke the loop invariant), it would loop infinitely
rather than stop with a wrong result.



More information about the Python-list mailing list