English subtitles

← Fix The Pseudocode - Intro to Java Programming

Get Embed Code
5 Languages

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

  1. If n is 123, count will end up as 3, which would be correct. There are three
  2. digits. If n is 100, count will end up as 2, which is not the correct number of
  3. digits. The loop condition should actually be. While temp is greater than or
  4. equal to 10. We can see this by hand tracing the code. The values I'm
  5. interested in are count and temp. If n starts out as 123, then count will start
  6. as 1, temp will start as 123. Temp is greater than 10, so we increment count.
  7. And divide temp by 10 and divide temp by 10.0 which will give us 12.3. Now, we
  8. go back to the loop condition. Temp is still greater then 10 so we increment
  9. the count and then divide temp by 10.0 again so we have 1.23. Now, when we
  10. check again, temp is less than 10. So we skip to the end, and we're done. But
  11. what would happen if n was 100? Count would start as 1, the way it did before.
  12. And temp would start as 100. Temp is greater than 10, so we would increment
  13. count and then divide temp by 10.0. So we would have 10. And then check the
  14. condition again. But this time, 10 is not greater than 10. It's equal, so it
  15. skips out immediately. When in fact, we still had two digits left. We needed to
  16. increment count again. This is why the condition should be temp is greater than
  17. or equal to 10.