AP Computer Science 1.1 Review of the Basics
Recommendation
Want a study guide too?
AP Computer Science: Review of the Basics Drill 1, Problem 1. What does this piece of code output?
AP Computer Science | Review of the Basics |
Computer Science | AP Computer Science |
Language | English Language |
Standard Operations and Algorithms | Searching |
Test Prep | AP Computer Science |
Transcript
Pretty simple Request We just have to prove to the
test and to ourselves that we know how to read
java code and figure out what it does Well it's
Not merely a pest problem This kind of thing is
done all the time when debugging program right but also
comes up quite a bit when software engineers have to
maintain or update pulled code Occasionally you'll have to work
with stuff that had been written five or ten years
ago or more perhaps by someone who is no longer
on the project and maybe they didn't leave comments in
the code so it's tough to figure out exactly what
it was meant to do or they thought they were
being really cute and they left their comments and cling
on Well good luck with that Anyway in this case
is just eight lines but we can already see that
there are a couple of loops that end up doing
an awful lot in that short space So let's go
through and decode this baby step by step All right
up top line one sets us up with manager i
with a value of five Go ahead and write that
down somewhere Scratch paper in your arms whatever we want
to keep track of where this variable takes us Line
two initiates a wild loop which will run as long
as an injured eye remains greater than zero so it's
five now and so it looks like we're good to
go Line three homeboy line three starts another loop nested
inside the first one and this time it's A four
loop Well four loops have three parameters The first being
the initialization imager J is one which is what we'll
be using is the eatery So write that on your
arm or whatever too The second is the termination parameter
When this condition is false the loop will no longer
run Yes j No longer being less than or equal
to I will stop the looting The third parameter is
called the increments and living up to its name It'll
increments j by one Each time the loop cycles Line
four is inside the loop It friends the product of
three times j and its base and he'll do it
over and over and over over within this loop until
j is no longer less than or equal to ay
well first time it runs Jay is one so we
print the product of three times one in a space
Then it'll increase j by one and a loop will
run again so next time it'll be three times too
and a space and then three times three and space
and three times for anna's face and three times five
in space And now jay six which is greater than
i look at your arms eyes five at the moment
That's the necessary condition for our four loop determining through
the chapel So now we move on to what happens
after the four loop which is an empty print line
statement that'll give us a new line and we subtract
one from i you all right But we're not done
The wild loop is still running And this time i
is for the interior Four loop is going to run
again and again until j is greater than i So
jay starts over at once and we'll print three times
one into space and three times do in the states
hand three times three spaces and three times for in
space but now has five which is greater than i
So we stopped the loop here and continue on with
an empty print line statement and decreasing i yet again
So from here we can totally tell what these loops
are doing and predict what will happen next The lupul
print three six nine and stop it because i will
be three And the local run again printing three six
and three At that point the wild loop will cease
running because i will no longer be greater than zero
Got all that and it looks like the result matches
up with the answer E and yeah that's our answer
and here's your smoke do sure brought to you by 00:04:37.173 --> [endTime] loops Wait not again Allright we're good out No