Table Of ContentS C
C O
AANN IILLLLUUSSTTRRAATTEEDD R V
LL EE AA RR NN TT OO A E
((AANNDD PPAAIINNLLEESSSS)) T R
C S
GGUUIIDDEE TTOO
H
CCOOMMPPUUTTEERR PP RR OO GG RR AA MM WW II TT HH 2
LL
SSCCIIEENNCCEE
EE
AA SS CC RR AA TT CC HH
RR
NN
Scratch is a fun, free, beginner-friendly programming • Read, store, and manipulate user input T T A V I S UA L I N TR O D U C T I ON T O P R O G R A M MI NG
environment where you connect blocks of code to build • Implement key computer science algorithms like linear OO W I T H G A M E S , A R T , S C I E N C E , A N D M A T H
programs. While most famously used to introduce kids
searches and bubble sorts
to programming, Scratch can make computer science PP
approachable for people of any age. Rather than type Hands-on projects will challenge you to create an RR
M A J E D M A RJ I
countless lines of code in a cryptic programming lan- Ohm’s law simulator, draw intricate patterns, program
OO
guage, why not use colorful command blocks and sprites to mimic line-following robots, create arcade-style
cartoon sprites to create powerful scripts? games, and more! Each chapter is packed with detailed GG
explanations, annotated illustrations, guided examples, RR
In Learn to Program with Scratch, author Majed Marji
lots of color, and plenty of exercises to help the lessons
uses Scratch to explain the concepts essential to solving AA
stick. Learn to Program with Scratch is the perfect place
real-world programming problems. The labeled, color- MM
to start your computer science journey.
coded blocks plainly show each logical step in a given
script, and with a single click, you can even test any WW
part of your script to check your logic. You’ll learn ABOUT THE AUTHOR
how to: II
Majed Marji is a senior development engineer at TT
• Harness the power of repeat loops and recursion General Motors and an adjunct faculty member at HH
Wayne State University in Michigan. He holds a PhD
• Use if/else statements and logical operators to make in electrical engineering from Wayne State University S S
decisions
and an MBA in strategic management from Davenport CC
University.
• Store data in variables and lists to use later in your RR
program
AA
TT
CC
HH
THE FINEST IN GEEK ENTERTAINMENT™
www.nostarch.com
M
A
$34.95 ($36.95 CDN) R
J
I
LANGUAGESCOMPUTERS/PROSHELVE IN:
G
RAM
M
IN
G
Learn to Program with Scratch
L e a r n t o
P r o g r a m w i t h
S c r a t c h
a Visual introduction
to Programming with games,
art, Science, and math
by Majed Marji
San Francisco
Learn to Program with Scratch. Copyright © 2014 by Majed Marji.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
Printed in USA
First printing
18 17 16 15 14 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-543-9
ISBN-13: 978-1-59327-543-3
Publisher: William Pollock
Production Editor: Alison Law
Cover Illustration: Tina Salameh
Developmental Editor: Jennifer Griffith-Delgado
Technical Reviewer: Tyler Watts
Copyeditor: Paula L. Fleming
Compositor: Lynn L’Heureux
Proofreader: Kate Blackham
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Marji, Majed, author.
Learn to program with Scratch : a visual introduction to programming with games, art, science, and
math / by Majed Marji.
pages cm
Audience: 11+
ISBN-13: 978-1-59327-543-3 (paperback)
ISBN-10: 1-59327-543-9 (paperback)
1. Scratch (Computer program language) 2. Computer programming. 3. Computer games--Programming.
I. Title.
QA76.73.S345M38 2014
794.8'1526--dc23
2013043492
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in it.
about the author
Majed Marji holds a PhD in electrical engineering from Wayne State Uni-
versity and an MBA in strategic management from Davenport University.
He has over 15 years of experience in the automotive industry, where he
developed many software applications for real-time data acquisition, device
control, test-cell management, engineering data analysis, embedded con-
trollers, telematics, hybrid vehicles, and safety-critical powertrain systems.
Dr. Marji is also an adjunct faculty member with the Electrical Engineering
Department at Wayne State University. He has taught courses on communi-
cation engineering, machine vision, microprocessors, control systems, and
algorithms and data structures, among other topics.
about the technical reviewer
Tyler Watts, EdS, is a creative-computing educator who teaches sixth
through eighth graders in Kansas City (Kansas) Unified School District 500
and adult students at the University of Missouri–Kansas City. He has been
using Scratch since 2009 as a tool to combat the digital divide and teach
students how to think like computer scientists. Since Tyler’s first year of teach-
ing Scratch, he has learned the importance of weaning learners off of the
“Scratch training wheels” and challenging them and molding them into
digital creators. He feels that programming is a form of personal expres-
sion and teaches his students to approach it as they would any other art
form and have fun.
Brief c ontentS
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Motion and Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 3: Looks and Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Chapter 4: Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Chapter 5: Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Chapter 6: Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Chapter 7: Repetition: A Deeper Exploration of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Chapter 8: String Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Chapter 9: Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Appendix: Sharing and Collaboration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251