Students
Teachers & SchoolsStudents
Teachers & SchoolsAPCS: Review of the Basics Drill 2, Problem 3. What is the output of the following code segment?
AP | AP Computer Science |
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 |
right And here your potential answers Okay true false got
it All right let's Go let's take a look at
what this code segment is trying to do First we
get two double variables The first being given the value
of ten and a second being given the value of
zero Then we start a loop The loop begins counting
with inger i zero halt when i stopped being less
than fifty and increases i buy one each time the
loop like once inside the loop will be adding point
to numb teo over and over and over again fifty
times total since that's how long our loop will run
afterwards we'll have three statements Print results of a few
operations were the first prints the tour false results of
whether numb one equals to the second print through her
false based on whether numb one divided by two equals
one same number and not zero And the third prince
true or false depending on whether numb one minus number
two equal zero again away to check if they're the
same number So let's try and predict the kinds of
results will get All right Back up to our loop
We've got numb to starting out equal to zero and
we add point two two it fifty times Well in
california fifty towns point two is ten so after the
loop is finished running canting treating tonight someone in them
too should both be tense And if we plug that
into our print statements we'd get numb One equals dumb
two or ten equals ten True can divide by ten
equals one true and minus ten equals zero True but
way Just hold your horses one sec If you got
your handy compiler you can pause the video here and
try running the code yourself and no seriously give the
shot will wait here's the code again and a picture
of a cold er Yesterday all my troubles seemed so
far ready All right So what did you find out
Well for those of you who didn't run the code
this is what happened Falls all falls which is our
answered the question by the way option he but wait
Why Tennis Ten right there's Not much That could possibly
go wrong with the statement Tennis Just ten even in
california with all you communists out there But i will
unless tennis somehow not ten Well let's add a line
to the end of the snippet to investigate a little
more closely We'll have it print the actual value of
numb to to make sure we're getting the ten that
we paid for system out print line none too And
go well ten is not ten it's Very very close
But it's just not in this case It's like nine
point nine nine nine nine nine six something that's why
our statements returned false and it's Not even a bug
issue here has to do with the very nature of
floating point arithmetic Those double variables we declared earlier and
more formally known as double precision floating point variables which
is to say is not quite as solid as an
imager it's More like an approximation of a real number
This has its uses when dealing with very complicated numbers
that don't quite need to be perfectly exact The distance
to the next galaxy over for example like that But
when you're dealing with the values that need to be
concrete like money do yourself a favor and compute using
interest or long otherwise you're going to be going people
One craw drill yin of a penny and your knives 00:03:32.22 --> [endTime] probably aren't that shark even against you