English subtitles

← While Loops Solution - Intro to Computer Science

Get Embed Code
9 Languages

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

  1. So the answer is: it prints out the numbers, from 1 to 10.
  2. And if we follow through the code, we can see why.
  3. So initially, the value of "i" is zero.
  4. The test says, well "i" is not equal to 10,
  5. so zero is not equal to 10--
  6. so we go through the Block,
  7. and the Block adds 1 to "i".
  8. So that will change the value of "i".
  9. Now "i" refers to "1".
  10. And then the next statement prints the value of "i".
  11. The value of "i" now is 1, so it will print, "1".
  12. And then, because it's a while, we go back.
  13. We do the test again--I'm not going to step through every time, so we keep doing this.
  14. As "i" gets bigger, we're going to keep going.
  15. Eventually--let's say "i" is 9,
  16. and by the time "i" is 9--well, after "i" was 9--we print "9".
  17. So we've printed the numbers from 1 through 9.
  18. At this point, "i" is still not equal to 10,
  19. so we still go through the loop body.
  20. Now we add 1 to "i"--that will make the value of "i" 10.
  21. Then we do the print, so that will print out, "10".
  22. Then we go back to the "while" test.
  23. Now the value of "i" is 10, so: "i" not equal to 10--"10" not equal to 10--
  24. is False, because 10 does equal 10.
  25. That means we're done with the "while" loop
  26. and we're going to continue,
  27. but there's nothing to continue with, so we're done.
  28. So the net result of the code is printing the numbers from 1 to 10
  29. and that's all that happens.
  30. So now the test is False.
  31. We don't execute the Block anymore, we would continue with the next statement.
  32. There isn't one, so we're done
  33. and what the code did was print out the numbers from 1 to 10.