[Tutor] confusion with else command

Dave Angel d at davea.name
Tue Feb 7 19:58:03 CET 2012


On 02/07/2012 01:52 PM, Debashish Saha wrote:
> for i in range(1, 8):
>      print(i)
>      if i==3:
>          break
> else:
>      print('The for loop is over')
>
>
> Output:
> 1
> 2
> 3
>
> Question:
>
> but after breaking the for loop why the else loop could not work?

It works fine.  The else clause of a for loop executes only if the loop 
completes without breaking out.

If the language designers had wanted it to always execute, why have an 
extra keyword?

-- 

DaveA



More information about the Tutor mailing list