Return to Video

Fix The Pseudocode - Intro to Java Programming

  • 0:00 - 0:05
    If n is 123, count will end up as 3, which would be correct. There are three
  • 0:05 - 0:09
    digits. If n is 100, count will end up as 2, which is not the correct number of
  • 0:09 - 0:16
    digits. The loop condition should actually be. While temp is greater than or
  • 0:16 - 0:20
    equal to 10. We can see this by hand tracing the code. The values I'm
  • 0:20 - 0:26
    interested in are count and temp. If n starts out as 123, then count will start
  • 0:26 - 0:33
    as 1, temp will start as 123. Temp is greater than 10, so we increment count.
  • 0:35 - 0:41
    And divide temp by 10 and divide temp by 10.0 which will give us 12.3. Now, we
  • 0:41 - 0:46
    go back to the loop condition. Temp is still greater then 10 so we increment
  • 0:46 - 0:52
    the count and then divide temp by 10.0 again so we have 1.23. Now, when we
  • 0:52 - 0:56
    check again, temp is less than 10. So we skip to the end, and we're done. But
  • 0:56 - 1:02
    what would happen if n was 100? Count would start as 1, the way it did before.
  • 1:02 - 1:06
    And temp would start as 100. Temp is greater than 10, so we would increment
  • 1:06 - 1:14
    count and then divide temp by 10.0. So we would have 10. And then check the
  • 1:14 - 1:19
    condition again. But this time, 10 is not greater than 10. It's equal, so it
  • 1:19 - 1:25
    skips out immediately. When in fact, we still had two digits left. We needed to
  • 1:25 - 1:29
    increment count again. This is why the condition should be temp is greater than
  • 1:29 - 1:30
    or equal to 10.
Title:
Fix The Pseudocode - Intro to Java Programming
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
cs046: Intro to Programming
Duration:
01:31
Udacity Robot edited English subtitles for 11-25 Fix The Pseudocode
Udacity Robot edited English subtitles for 11-25 Fix The Pseudocode
Udacity Robot edited English subtitles for 11-25 Fix The Pseudocode
Cogi-Admin edited English subtitles for 11-25 Fix The Pseudocode
Cogi-Admin added a translation

English subtitles

Revisions Compare revisions