Advertisement
B1KMusic

Figuring out while..else

Feb 7th, 2015
2,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. >>> while False:
  2. ...   break
  3. ... else:
  4. ...   print(1)
  5. ...
  6. 1
  7. >>> while False:
  8. ...   break
  9. ... else:
  10. ...   print("asdf")
  11. ...
  12. asdf
  13.  
  14. # So I know the "1" is coming from the else block, and not some return value
  15.  
  16. >>> while True:
  17. ...   break
  18. ... else:
  19. ...   print(1)
  20. ...
  21. >>> for i in range(0):
  22. ...   break
  23. ... else:
  24. ...   print(1)
  25. ...
  26. 1
  27. >>> for i in range(1):
  28. ...   break
  29. ... else:
  30. ...   print(1)
  31. ...
  32. >>> for i in range(100):
  33. ...   break
  34. ... else:
  35. ...   print(1)
  36. ...
  37.  
  38. # Seems useless so far
  39.  
  40. >>> i = 100
  41. >>> while i: # Test while 100 -> 0 (first i is true)
  42. ...   i -= 1
  43. ... else:
  44. ...   print(1)
  45. ...
  46. 1
  47. >>> while i: # test while 0 (first i is false)
  48. ...   pass
  49. ... else:
  50. ...   print(1)
  51. ...
  52. 1
  53. >>> while i < 100: # test while 0 -> 99 (first i < 100 is true)
  54. ...   i += 1
  55. ... else:
  56. ...   print(1)
  57. ...
  58. 1
  59. >>> while 1: # (always true)
  60. ...   break
  61. ... else:
  62. ...   print(1)
  63. ...
  64.  
  65. # So with while, it always executes else blocks, unless you're specifically breaking out of an infinite loop?
  66.  
  67. >>> i = 0
  68. >>> while i < 100:
  69. ...  i+=1 # let the loop complete
  70. ... else:
  71. ...  print(1)
  72. ...
  73. 1
  74. >>> i = 0
  75. >>> while i < 100:
  76. ...   break # kill the loop while condition is still true
  77. ... else:
  78. ...   print(1)
  79. ...
  80. >>> for i in range(100):
  81. ...   pass # let it run
  82. ... else:
  83. ...   print(1)
  84. ...
  85. 1
  86. >>> for i in range(100):
  87. ...   break # kill it while true
  88. ... else:
  89. ...   print(1)
  90. ...
  91.  
  92.  
  93. # Now it makes sense. When it gets to the else statement, it checks the loop's condition one last time, and decides, based on that, whether to run the else block.
  94.  
  95. # I can't really see a use for this. In fact, when trying to come up with something, I found that an if..else did MORE than the single else block was able to.
  96.  
  97. >>> i = 0
  98. >>> while i < 100:
  99. ...   i += 1
  100. ... else:
  101. ...   print("completed successfully")
  102. ...
  103. completed successfully
  104. >>> i = 0
  105. >>> while i < 100:
  106. ...   break
  107. ... else: # uh oh
  108.  
  109. # Aside from the use of an else block to say "success" being logically questionable, this only allows to debug a success/failure assuming you are willing to print just before every break. This is fine if you want to be specific, so that you can single out an error among many, but if the only thing you need to know is that it failed, and you want to do that with a single print line, this method cannot provide the means, whereas a simple if..else...
  110.  
  111. if not i < 100:
  112.   print("success")
  113. else:
  114.   print("failure")
  115.  
  116. # ...can.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement