Advertisement
webmanoffesto

Udacity CS101 Unit2-26 Break Solution and Notes

Apr 22nd, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. Udacity CS101, Unit 2-26, Equivalent Loops Quiz, Spoiler Alert - My notes after watching the solution to this quiz - Udacity Forums
  2. This was posted to the Udacity Forum at http://forums.udacity.com/questions/100051420/spoiler-alert-my-notes-after-watching-the-solution-to-this-quiz#cs101
  3. -----
  4.  
  5. print "Udacity Quiz, Lesson 2, Section 26"    
  6. print "The basic 'while' loop"
  7. print "- Quiz: https://www.udacity.com/course/viewer#!/c-cs101/l-48753036/e-48714355/m-48684618"
  8. print "- Answer: https://www.udacity.com/course/viewer#!/c-cs101/l-48753036/e-48714355/m-48700404"
  9. print "A good link regarding 'loop control': break, continue, and pass"
  10. print "http://www.tutorialspoint.com/python/python_loop_control.htm"
  11.  
  12. print ""
  13. print "The original"
  14. print "Which of the below produce the same output as this."
  15. i = 0
  16. variable1 = 3
  17. while i <= variable1:
  18.     i = i + 1
  19.     print i
  20. print""
  21.  
  22. print "1. Using 'break'"
  23. while i <= variable1:
  24.     if False:
  25.         break
  26.     i = i + 1
  27.     print i
  28. print "This is equivalent because it will never 'break'."
  29. print""
  30.  
  31. print "2. While True"
  32. print "not equivalent because it prints 1 and then 'breaks'"
  33. i = 0
  34. variable1 = 3
  35. while i <= variable1:
  36.     i = i + 1
  37.     print i
  38.     break
  39. print""
  40.  
  41. print "3. Using 'break'"
  42. i = 0
  43. variable1 = 3
  44. while True:
  45.     if i <= variable1:
  46.         break
  47.     i = i + 1
  48.     print i
  49. print "This is not equivalent, because in the original the "
  50. print "command is executed 'while' the test is true."
  51. print "in this (#3) it 'breaks' if the test is true"
  52. print "so (in this case) it will not print anything."
  53. print""
  54.  
  55. print "'Correcting' #3. 'if not' "
  56. print "So now it is equivalent"
  57. i = 0
  58. variable1 = 3
  59. while True:
  60.     if not i <= variable1: # "if i" changed to "if not i"
  61.         break
  62.     i = i + 1
  63.     print i
  64. print " 'if i <= variable1:' changed to 'if not i <= variable1:' "
  65. print""
  66.  
  67.  
  68.  
  69. print "4. Using 'break'"
  70. i = 0
  71. variable1 = 3
  72. while i <= variable1:
  73.     i = i + 1
  74.     print i, "output by main loop"
  75.     if i <= variable1:
  76.        i = i + 1
  77.        print i, "output by 'if' statement in sub-loop"
  78.     else:
  79.         break
  80. print "This is equivalent, because the 'if statement'"
  81. print "sub-loop simply does the same work as the main loop"
  82. print "the main loop prints 1, the sub loop prints 2, then the main loop prints 3"
  83. print""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement