<div>If I'm reading the indentation correctly, the else is applying to the for loop, not the if statement.</div><div><br></div><div>When used in this way, the else occurs only if the for loop exits due to completion (aka, the for loop does not exit due to a break or return statement).</div>
<div><br></div><div><br></div><div>I would expect the output from that code to be:</div><div>0</div><div>1</div><div>2</div><div>3</div><div>7</div><div><br></div><div>Any values in the range under 4 should be printed (the if), as well as the last value in the range (7 in the case given).</div>
<div><br></div><br clear="all">Chris<br>
<br><br><div class="gmail_quote">On Tue, Sep 29, 2009 at 8:50 AM, Sandy <span dir="ltr"><<a href="mailto:dksreddy@gmail.com">dksreddy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi all,<br>
A simple and silly if-else question.<br>
I saw some code that has the following structure. My question is why<br>
else is used there though removing else<br>
has the same result. More important, is it not syntactically wrong :-(<br>
<br>
for i in xrange(8):<br>
    if i < 4:<br>
        print i<br>
else:<br>
    print i<br>
<br>
Cheers,<br>
dksr<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>