English subtitles

← Break Solution - Intro to Computer Science

Get Embed Code
8 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. Here's the answer.
  2. The first one is equivalent.
  3. The reason for that is we have our code here that has "if False."
  4. "False" will never be true, so we'll never actually run the "break"âÂÂ
  5. we'll never reach that code.
  6. We'll just run the statement again and we'll have the same thing that we started with.
  7. Every time the test is true, we execute the statement.
  8. The second one is not equivalent, and the reason for that is the "break" will
  9. stop the loop.
  10. If the loop would execute more than once in this version,
  11. and if we go through the loop twice,
  12. in this case we can only ever go through the statement once,
  13. since after we go through the statement once, we reach the "break" and then we're done.
  14. The third one is not equivalent, so here we're having a "while true" that will run
  15. forever except for we have the "break" and that would stop it.
  16. The problem is the test condition for the break is the same as the
  17. test condition for the "while," but it should really be the opposite.
  18. In the "while" we keep going as long as the test condition is true.
  19. The way I have written in the code here, if the test condition is true, we stop.
  20. For this to be equivalent, what we should have written instead
  21. would've taken the opposite of the test condition, so if we had "if" not"âÂÂ
  22. which changes true to false and false to true, then the test condition,
  23. then the breakâÂÂthis would be equivalent to the original loop.
  24. What we have is not.
  25. The final one is the most complicated.
  26. This one is actually always equivalent to the original loop.
  27. The reason for that is if we think through the execution, so in the
  28. original loop, if the test condition is true, we ask to give the statement "S,"
  29. and then we keep going.
  30. If the test condition is true the second time, we execute "S" again
  31. and we keep going.
  32. If we look at what happens hereâÂÂwell, if the test condition is true, we
  33. execute "S."
  34. Then if the test condition is true again, we execute S and we keep going.
  35. That has the same behavior.
  36. We should trace it through the case where the test condition is
  37. not true.
  38. HereâÂÂif the test condition was true, we execute "S."
  39. If it's not true the second time, then we're done with the loop
  40. and we continue.
  41. In this case if the test condition is true the first time, we'll execute "S."
  42. If it's not true the second time, then we would go to the "else."
  43. So if it's not true, we go to the "else" and then we go to the "break" and
  44. the "break" gets out of the loop.
  45. We have the same exact behavior where we executed "T" once, then
  46. we executed "S," and then we executed "T."
  47. We didn't execute "S" again, and we broke out of the loop to continue.
  48. So these two are equivalent and the other two are different.