'while/else' (ugh) (was Re: [oclug] Ruby)
rod at giffinscientific.com
Fri Jun 27 13:19:06 EDT 2003
Rod Giffin said:
> David F. Skoll said:
>> On Fri, 27 Jun 2003, Rod Giffin wrote:
>>> > while not found:
>>> > line = fp.readline()
>>> > if not line: break
>>> > if re.search(pattern, line):
>>> > found = True
>>> > else:
>>> > print "we didn't find the pattern"
>> Am I the only one who finds that repugnant?
>> When, exactly, does the "else" fire? If the loop is iterated zero
>> times? If the loop is ever exited with "found" still false?
> The else would not fire, unless "found" was true when you attempted to
> enter the loop.
I bet I'm wrong. The logic of this is awkward. I bet it would also run
if the condition of the loop was met, and the break statement was not
encountered, so the else would run if the search pattern was found, which
would be a logic error.
More information about the OCLUG