How to Think Like a Computer Scientist
Allen B. Downey
Copyright c 2011 Allen Downey.
Permission is granted to copy, distribute, and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation;with Invariant Sections being
“Preface”, with no Front-Cover Texts, and with no Back-Cover Texts. A copy
of the license is included in the appendix entitled “GNU Free Documentation
The GNU Free Documentation License is available from www.gnu.org or by
writing to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307, USA.
The original form of thisbook is L TEX source code. Compiling this L TEX
source has the eﬀect of generating a device-independent representation of the
book, which can be converted to other formats and printed.
The L TEX source for this book is available from
This book was typeset using L TEX. The illustrations were drawn in xﬁg. All
of these are free, open-source programs.
“Aswe enjoy great Advantages from the Inventions of others, we
should be glad of an Opportunity to serve others by any Invention
of ours, and this we should do freely and generously.”
—Benjamin Franklin, quoted in Benjamin Franklin by Edmund S.
Why I wrote this book
This is the fourth edition of a book I started writing in 1999, when I was teaching
at Colby College. I had taught anintroductory computer science class using the
Java programming language, but I had not found a textbook I was happy with.
For one thing, they were all too big! There was no way my students would read
800 pages of dense, technical material, even if I wanted them to. And I didn’t
want them to. Most of the material was too speciﬁc—details about Java and its
libraries that would be obsolete by theend of the semester, and that obscured
the material I really wanted to get to.
The other problem I found was that the introduction to object oriented programming was too abrupt. Many students who were otherwise doing well just
hit a wall when we got to objects, whether we did it at the beginning, middle
So I started writing. I wrote a chapter a day for 13 days, and on the 14th day Iedited. Then I sent it to be photocopied and bound. When I handed it out on
the ﬁrst day of class, I told the students that they would be expected to read
one chapter a week. In other words, they would read it seven times slower than
I wrote it.
The philosophy behind it
Here are some of the ideas that made the book the way it is:
• Vocabulary is important. Students need to be able to talkabout programs
and understand what I am saying. I tried to introduce the minimum
number of terms, to deﬁne them carefully when they are ﬁrst used, and
to organize them in glossaries at the end of each chapter. In my class, I
include vocabulary questions on quizzes and exams, and require students
to use appropriate terms in short-answer responses.
• In order to write aprogram, students have to understand the algorithm,
know the programming language, and they have to be able to debug. I
think too many books neglect debugging. This book includes an appendix
on debugging and an appendix on program development (which can help
avoid debugging). I recommend that students read this material early and
come back to it often.
• Some concepts take time to sink in. Someof the more diﬃcult ideas in
the book, like recursion, appear several times. By coming back to these
ideas, I am trying to give students a chance to review and reinforce or, if
they missed it the ﬁrst time, a chance to catch up.
• I try to use the minimum amount of Java to get the maximum amount of
programming power. The purpose of this book is to teach programming
and some introductory...