Learn to Program with Small Basic: An Introduction to Programming with Games, Art, Science, and Math
By Majed Marji and Ed Price
()
About this ebook
Learn to Program with Small Basic introduces you to the empowering world of programming. You’ll master the basics with simple activities like displaying messages and drawing colorful pictures, and then work your way up to programming games! Learn how to:
–Program your computer to greet you by name
–Make a game of rock-paper-scissors using If/Else statements
–Create an interactive treasure map using arrays
–Draw intricate geometric patterns with just a few lines of code
–Simplify complex programs by breaking them into bite-sized subroutines
You’ll also learn to command a turtle to draw shapes, create magical moving text, solve math problems quickly, help a knight slay a dragon, and more!
Each chapter ends with creative coding challenges so you can take your skills to the next level. Learn to Program with Small Basic is the perfect place to start your computer science journey.
Related to Learn to Program with Small Basic
Related ebooks
Getting Started with Coding: Get Creative with Code! Rating: 0 out of 5 stars0 ratingsYou Can't Dance to These Rhythms: What Are Algorithms? Rating: 0 out of 5 stars0 ratingsCoding, Bugs, and Fixes Rating: 4 out of 5 stars4/5Helping Kids with Coding For Dummies Rating: 0 out of 5 stars0 ratingsLearn to Program Rating: 1 out of 5 stars1/5C Programming For Dummies Rating: 0 out of 5 stars0 ratingsSoftware Development Techniques Rating: 0 out of 5 stars0 ratingsLearn Coding Basics in Hours with Small Basic Rating: 0 out of 5 stars0 ratingsArtificial Intelligence Class 9 Rating: 0 out of 5 stars0 ratingsSimple Coding Rating: 0 out of 5 stars0 ratingsNothing Loopy about This: What Are Loops and Conditionals? Rating: 0 out of 5 stars0 ratingsCode-It Workbook 2: Choices In Programming Using Scratch Rating: 0 out of 5 stars0 ratingsAlgorithms Made Simple: Understanding the Building Blocks of Software Rating: 0 out of 5 stars0 ratingsFoundations of Computing: Essential for Computing Studies, Profession And Entrance Examinations - 5th Edition Rating: 0 out of 5 stars0 ratingsTouchCode Class 3 Rating: 0 out of 5 stars0 ratingsProgramming for Everyone Rating: 3 out of 5 stars3/5Create Computer Games with Scratch Rating: 0 out of 5 stars0 ratings200+ JavaScript Programs for Beginners Rating: 0 out of 5 stars0 ratingsPython World War : Programming Fundamentals For Students: Python World war : programming Fundamentals series, #1 Rating: 0 out of 5 stars0 ratingsLearning C++ by Creating Games with UE4 Rating: 3 out of 5 stars3/5Code Foundations Rating: 0 out of 5 stars0 ratingsProgramming Best Practices for New Developers: A Practical Guide with Examples Rating: 0 out of 5 stars0 ratingsGo Programming - From Beginner to Professional: Learn everything you need to build modern software using Go Rating: 0 out of 5 stars0 ratingsLearning Primary Programming with Scratch (Home Learning Book Years 5-6) Rating: 0 out of 5 stars0 ratingsTrackpad Pro Ver. 5.0 Class 5: WINDOWS 11 & MS OFFICE 2021 Rating: 0 out of 5 stars0 ratingsCoding Education Shifts Rating: 0 out of 5 stars0 ratingsC# and Algorithmic Thinking for the Complete Beginner: Unlock the Power of Programming with C# and Algorithmic Thinking Rating: 0 out of 5 stars0 ratingsBasic Language Rating: 0 out of 5 stars0 ratingsTrackpad Ver. 2.0 Class 3 Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5C Programming Language, A Step By Step Beginner's Guide To Learn C Programming In 7 Days. Rating: 4 out of 5 stars4/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Algorithms For Dummies Rating: 4 out of 5 stars4/5Windows 11 For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for Learn to Program with Small Basic
0 ratings0 reviews
Book preview
Learn to Program with Small Basic - Majed Marji
ABOUT THE AUTHORS
Majed Marji is a senior development engineer at General Motors and an adjunct faculty member at Wayne State University in Michigan. He is also the author of Learn to Program with Scratch (No Starch Press).
Ed Price is a senior program manager in engineering at Microsoft. He holds an MBA in technology management and has been a professor at Bellevue College. He runs customer feedback programs for Azure Development, Visual Studio, and Small Basic.
BRIEF CONTENTS
Foreword by Vijaye Raji
Acknowledgments
Introduction
Chapter 1: Introducing Small Basic
Chapter 2: Getting Started
Chapter 3: Drawing Basics
Chapter 4: Using Variables
Chapter 5: Drawing Shapes with Turtle Graphics
Chapter 6: Getting User Input
Chapter 7: Empowering Programs with Math
Chapter 8: Making Decisions with If Statements
Chapter 9: Using Decisions to Make Games
Chapter 10: Solving Problems with Subroutines
Chapter 11: Event-Driven Programming
Chapter 12: Building Graphical User Interfaces
Chapter 13: Repeating For Loops
Chapter 14: Creating Conditional While Loops
Chapter 15: Grouping Data in One-Dimensional Arrays
Chapter 16: Storing Data with Associative Arrays
Chapter 17: Expanding to Higher-Dimension Arrays
Chapter 18: Advanced Text Magic
Chapter 19: Receiving File Input and Output
Where to Go from Here
Index
CONTENTS IN DETAIL
FOREWORD by Vijaye Raji
ACKNOWLEDGMENTS
INTRODUCTION
Who Should Read This Book?
What’s in This Book?
Online Resources
A Note to the Reader
1
INTRODUCING SMALL BASIC
What Is a Computer?
What Is a Computer Program?
What Is Small Basic?
The Vision of Small Basic
The Basics of Small Basic
The Small Basic Language
The Small Basic Library
The Small Basic Development Environment
Installing Small Basic
The Small Basic IDE
Opening and Saving Your Work
Sharing Your Work and Importing Games
Copy and Paste; Undo and Redo
Running Your Program and Graduating
Writing and Running Your First Program
Objects and Methods
Naming Your Programs
Files Generated by Small Basic
Helping Hands: IntelliSense and Syntax Coloring
Drawing with Small Basic
Try It Out 1-1
Programming Challenges
2
GETTING STARTED
The Parts of a Program
Comments and Statements
Characters and Strings
Arguments and Methods
Try It Out 2-1
Exploring Other Features
Case Sensitivity
Sequential Execution
Displaying Numbers and Doing Math
Joining Strings
Try It Out 2-2
Object Properties
Setting and Changing Property Values
Working with Properties
Try It Out 2-3
Arithmetic Operators
Try It Out 2-4
Programming Errors
Syntax Errors
Logic Errors
Runtime Errors
Programming Challenges
3
DRAWING BASICS
The Graphics Coordinate System
Drawing Lines
Drawing Shapes
Triangles
Try It Out 3-1
Rectangles and Squares
Try It Out 3-2
Ellipses and Circles
Pen Size and Color
Pen Width and Shape Size
Try It Out 3-3
Drawing Text
Inserting Images
Programming Challenges
4
USING VARIABLES
What’s a Variable?
The Basics of Using Variables
Assigning Expressions to Variables
Passing Variables to Methods
Changing the Value of a Variable
Using Spaces for Readability
Try It Out 4-1
Rules for Naming Variables
Say What You Mean
Find the Right Length
Stick with Your Style
Let IntelliSense Work for You
Avoid Naming Variables After Methods and Objects
Try It Out 4-2
Simplifying Expressions
Try It Out 4-3
Using Variables to Solve Problems
Try It Out 4-4
Two Kinds of Data
Global Variables
Try It Out 4-5
Programming Challenges
5
DRAWING SHAPES WITH TURTLE GRAPHICS
Meet the Turtle
Moving the Turtle
Absolute Motion
Relative Motion
Coloring Your Steps
Controlling Your Speed
Try It Out 5-1
Introducing the For Loop
Try It Out 5-2
Drawing Regular Polygons
A Star Is Born
Try It Out 5-3
Creating Polygon Art Using Nested Loops
Try It Out 5-4
Endless Graphics
Try It Out 5-5
Programming Challenges
6
GETTING USER INPUT
Talking to the Computer
Your Number, Please?
Introducing Yourself to Your Computer
Writing Prompts for Input
A Moment of Silence, Please (Pause)
Working with User Input
Converting Fahrenheit to Celsius
Try It Out 6-1
Averaging Numbers
Reading Text
Try It Out 6-2
Programming Challenges
7
EMPOWERING PROGRAMS WITH MATH
Exponent Methods
SquareRoot() and Good Old Pythagoras
Powerful Powers
Try It Out 7-1
Rounding Methods
Traditional Rounding
Rounding to the Nearest Hundredth
Try It Out 7-2
Abs(), Min(), and Max() Methods
Try It Out 7-3
The Remainder() Method
Try It Out 7-4
Random Numbers
Try It Out 7-5
Trigonometric Methods
Try It Out 7-6
Programming Challenges
8
MAKING DECISIONS WITH IF STATEMENTS
The If Statement
Relational Operators
Try It Out 8-1
Complex If Conditions
Try It Out 8-2
Comparing Strings
The If/Else Statement
Try It Out 8-3
Nested If and If/Else Statements
Try It Out 8-4
The Goto Statement
Try It Out 8-5
Programming Challenges
9
USING DECISIONS TO MAKE GAMES
The If/ElseIf Ladder
Letter Grades
The Bug on the Ladder
Try It Out 9-1
Let’s Get Logical
Logical Operators in the Zoo
The And Operator
The Or Operator
The Cosmic Order of Evaluation
Try It Out 9-2
The Shapes Object
Try It Out 9-3
Create a Game: Guess My Coordinates
Step 1: Open the Startup File
Step 2: Set Up the Game
Step 3: Hide the Star
Step 4: Let the User Guess
Try It Out 9-4
Programming Challenges
10
SOLVING PROBLEMS WITH SUBROUTINES
Why Use Subroutines?
Writing Subroutines
Try It Out 10-1
Subroutine Input and Output
Try It Out 10-2
Nesting Subroutines
Try It Out 10-3
Create a Dragon Game
Step 1: Open the Startup File
Step 2: Write the SetUp() Subroutine
Step 3: Add a Bit of Chance
Step 4: Let the Player Know What’s Going On
Step 5: Get the Player in the Game with GetChoice()
Step 6: Process the Player’s Choice
Step 7: Add Motion with MoveKnight()
Step 8: Shoot Arrows with ShootArrow()
Step 9: Swing the Sword with StabDragon()
Step 10: Breathe Fire
Try It Out 10-4
Programming Challenges
11
EVENT-DRIVEN PROGRAMMING
GraphicsWindow Events
Create Patterns with the MouseDown Event
Try It Out 11-1
Fire Missiles with the KeyDown Event
Try It Out 11-2
Make a Typewriter Using the TextInput Event
Try It Out 11-3
Draw Pictures with the MouseMove Event
Try It Out 11-4
Useful Tips
Create a Gold Rush Game
Step 1: Open the Startup File
Step 2: Move the Turtle
Step 3: Move the Bag of Gold
Step 4: Update the User’s Score
Try It Out 11-5
Programming Challenges
12
BUILDING GRAPHICAL USER INTERFACES
Design a User Interface with the Controls Object
Step 1: The Design Phase
Step 2: Program Interactivity
Try It Out 12-1
Make a Colorful Drawing Program
Try It Out 12-2
Explore Circuits with Code
Step 1: Open the Startup File
Step 2: Add the Main Code
Step 3: Toggle the Switch
Step 4: Respond to Changes
Step 5: Update the Program’s Interface
Try It Out 12-3
Program Your Own Image Viewer
Try It Out 12-4
Programming Challenges
13
REPEATING FOR LOOPS
The For Loop
Try It Out 13-1
Magical Moving Text
Try It Out 13-2
Adding ’em Up
Try It Out 13-3
Formatting Your Output
Try It Out 13-4
Drawing All Kinds of Lines
Try It Out 13-5
Changing the Step Size
Counting Down by Twos
Making a Fractional Step
Try It Out 13-6
Nested Loops
Tessellating for Fun
Try It Out 13-7
Multiple Nesting Levels
Try It Out 13-8
Programming Challenges
14
CREATING CONDITIONAL WHILE LOOPS
When to Use While Loops
Writing a While Loop
Try It Out 14-1
Validating Your Inputs
Try It Out 14-2
Infinite Loops
Try It Out 14-3
Create a Rock-Paper-Scissors Game
Step 1: Open the Startup File
Step 2: Add the MouseDown Handler
Step 3: Switch the Images
Step 4: Announce the Winner
Try It Out 14-4
Programming Challenges
15
GROUPING DATA IN ONE-DIMENSIONAL ARRAYS
Getting Started with Indexed Arrays
Array Basics
Initializing Arrays
Try It Out 15-1
Filling Arrays with a For Loop
Constant Initialization
Random Initialization
Formula Initialization
User Initialization
Try It Out 15-2
Displaying Arrays
Try It Out 15-3
Processing Arrays
Finding the Sum
Finding the Maximum Element
Using String Values in Arrays
Try It Out 15-4
Saving Records
Using Indexed Arrays
Random Selection
A Magic 8 Ball
Try It Out 15-5
Create the Catch Apples Game
Step 1: Open the Startup File
Step 2: Add the Apples
Step 3: Position the Apples
Step 4: Move the Apples
Step 5: Catch or Miss
Try It Out 15-6
Programming Challenges
16
STORING DATA WITH ASSOCIATIVE ARRAYS
Associative Arrays
Putting Associative Arrays to Use
Days in French
Try It Out 16-1
Storing Records
Try It Out 16-2
The Array Object
Is It an Array?
How Big Is an Array?
Does It Have a Particular Index?
Does It Have a Particular Value?
Give Me All the Indices
Try It Out 16-3
Your Computer the Poet
Step 1: Open the Startup File
Step 2: Set Up the Graphical User Interface
Step 3: Respond to Button Clicks
Step 4: Write the Poem’s First Line
Step 5: Write the Poem’s Second and Third Lines
Try It Out 16-4
Programming Challenges
17
EXPANDING TO HIGHER-DIMENSION ARRAYS
Two-Dimensional Arrays
A Random Matrix
Try It Out 17-1
A Matrix with User Input
Animated Squares
Try It Out 17-2
Using String Indices
Try It Out 17-3
Going Interactive
Try It Out 17-4
Common Operations on Numerical 2D Arrays
Step 1: Add All Elements
Step 2: Find the Sum of Each Column
Try It Out 17-5
Arrays of Three or More Dimensions
Try It Out 17-6
Create a Treasure Map Game
Step 1: Open the Startup File
Step 2: Create the GUI Elements
Step 3: Start a New Game
Step 4: Create a New Treasure Map
Step 5: Draw Objects on the Map
Step 6: Show the Player’s Location
Step 7: Handle Button Clicks
Try It Out 17-7
Programming Challenges
18
ADVANCED TEXT MAGIC
The Text Object
Appending Strings and Getting Their Length
Try It Out 18-1
Taking Strings Apart: Substrings
Try It Out 18-2
Changing Case
Try It Out 18-3
Character Coding with Unicode
Try It Out 18-4
Practical Examples with Strings
Counting Special Characters
Try It Out 18-5
Palindrome Number Checker
Try It Out 18-6
Igpay Atinlay
Try It Out 18-7
Fix My Spelling
Try It Out 18-8
Unscramble
Try It Out 18-9
Rhyme Time: The House That Jack Built
Try It Out 18-10
Programming Challenges
19
RECEIVING FILE INPUT AND OUTPUT
The Case for Files
Naming Files
File Organization
The File Object
File I/O Methods
Try It Out 19-1
Try It Out 19-2
File Management
Practical Programs
The Poet
Math Wizard
Try It Out 19-3
Programming Challenges
WHERE TO GO FROM HERE
INDEX
FOREWORD
Computer programming is magical. Programmers make computers come to life, and with the right tools, the possibilities are limitless. But even today’s skilled programmers started with something very small and simple.
I have often wondered what exactly makes programming exciting. The answer, I think, is how even tiny programs can make things happen instantly. When I was twelve, I was introduced to BASIC for the first time. The sheer simplicity of it struck me. It was inviting, not intimidating. And at the time, BASIC was everywhere—it had taken the new world of microcomputers by storm.
My first program worked flawlessly and gave me the instant gratification that kept me wanting to do more. This is what it looked like:
10 PRINT Hello
20 GOTO 10
These two lines of code just printed Hello again and again and again—but that’s all it took to get me hooked.
Fifteen years later, while working at Microsoft, I signed up to teach programming to a group of fifth graders. It was then I realized that as programming as a discipline had gotten more and more sophisticated, the simplicity of BASIC had been lost. There was no easy way for children to experience the same instant gratification that I experienced using BASIC.
When researching tools that could make learning computer science fun and welcoming, I stumbled across the article Why Johnny Can’t Code
¹). The article, published by Salon in 2006, argues that today’s children are missing out on computer science because there aren’t easy-to-use languages like BASIC readily available.
This inspired me to create Small Basic, a simple and easy way for children to learn programming.
But it’s been far from an individual effort. Since launching Small Basic in 2011, the outpouring of support from the community has been incredible. The programming environment has extended in ways I couldn’t have imagined—sensing hands and faces with Kinect, talking to robots, and even integrating with enterprise databases. Small Basic has been translated into more than twenty languages, and at the time of this writing, more than 280,000 programs have been uploaded to http://www.smallbasic.com/ by children around the world.
The original vision of Small Basic is now being carried on by Ed Price and Michael Scherotter, along with the support of active community members like Nonki, LitDev, and many more.
Majed Marji is no stranger to teaching programming. Following his success with Learn to Program with Scratch, I was really excited to hear he was going to author this book along with Ed Price. I’m happy to see Learn to Program with Small Basic come alive and be available for everyone who wants to take their first step into programming. The book does a great job of introducing beginners to programming concepts while keeping the content fun and engaging.
Welcome to the magical world of programming. What do you want to create today?
Vijaye Raji
Creator of Small Basic
Director of Engineering, Facebook
Former Principal Software Architect, Microsoft
ACKNOWLEDGMENTS
This book represents the culmination of hard work from many people to whom I owe my thanks. I’d like to begin by recognizing the editorial and production departments at No Starch who have done an outstanding job in bringing out this book to life. In particular, it is a pleasure to acknowledge the help of our editor, Hayley Baker, and our production editor, Laurel Chun, who worked hard to ensure that everything came together like it was supposed to.
I’d also like to thank the book’s technical editor, Vijaye Raji, for his thorough review and numerous constructive comments. Many thanks go to my coauthor, Ed Price, for making this book far better than it could have been without him.
And above all, my gratitude goes to my wife, Marina, and my two sons, Asad and Karam, who provided endless personal support. To them I owe an apology for the time this project has taken and my greatest thanks. The book is theirs too, and so is the spirit behind it. I dedicate this book to them as a sincere expression of appreciation and love.
Majed Marji
Firstly, I’d like to thank Majed for bringing me on this amazing journey with him. In college, I had a tough time learning C++, so with this book, Majed and I tried to write the programming learning experience that I wish I had had when I started programming.
I found Small Basic in 2012, while helping run Microsoft’s MSDN forums, and was excited to see that the community was being led by the teachers. Thank you to Vijaye for creating Small Basic and to the Small Basic community for their passion, which inspired me to write for the Small Basic blog and work on two new releases of Small Basic with Kinect and LEGO® MINDSTORMS® support.
Thanks to my wife, my four daughters, and my infant son, Asher, who was raised during this book. I’d also like to thank Tyler Ortman and Bill Pollock from No Starch Press for their support and for sticking with us.
I dedicate this book to the Small Basic team (Michael, Ray, Li Xin, François, Deva, and Liz) and Community Council (Nonki, Steve, Rick, Yan, and Liam) for keeping the dream going!
Ed Price
INTRODUCTION
Have you ever wondered how people create computer programs? Have you ever wanted to conjure up your own video game? Have you ever flipped through a programming book and been discouraged by its dull language and boring examples? If so, there may be a computer programmer lurking inside you, waiting to be unleashed. Welcome to Learn to Program with Small Basic!
Microsoft Small Basic is a free, text-based programming language designed for beginners. It provides a complete programming environment that helps you write, test, and fine-tune your creations. This book shows you how to install Small Basic and how to use it to do amazing things. We’ll show you that programming can be fun, rewarding, and—best of all—easy!
Who Should Read This Book?
You! This book introduces you to Small Basic in a fun, engaging, and interactive way. We offer an abundance of sample programs that you can run, explore, and tweak to make your own. Try every exercise and dig in to the extra online resources, review questions, and practice exercises. By the time you’re done with this book, you’ll be creating your own games!
If you’re feeling inspired, you can share your Small Basic creations on the Small Basic MSDN forum, where the Small Basic Community Council will be waiting to answer any questions and check out all your awesome work.
What’s in This Book?
Each chapter builds on the last one to help you hone your programming skills. We’ll start you with the basics, and by the end, you’ll be a total whiz!
• Chapter 1: Introducing Small Basic explains Small Basic’s features and gets you set up. Then you’ll create your first program.
• Chapter 2: Getting Started walks you through creating simple programs using Small Basic’s built-in text window.
• Chapter 3: Drawing Basics shows you how to write programs that draw shapes in the graphics window.
• Chapter 4: Using Variables explains how variables keep track of information. Variables play a huge role in programming, and you’ll use them throughout the book.
• Chapter 5: Drawing Shapes with Turtle Graphics teaches you how to command your own artistic turtle. You’ll draw intricate geometric shapes and patterns that would be tiresome to do by hand.
• Chapter 6: Getting User Input shows you how to bring your programs to life by making them interactive. You’ll write a program that can greet you by name.
• In Chapter 7: Empowering Programs with Math, you’ll use math to make games, like a dice game that uses a random number generator.
• Chapter 8: Making Decisions with If Statements shows you how to control the logic and flow of your programs. With If statements under your belt, you’ll be able to create even more powerful and exciting programs.
• In Chapter 9: Using Decisions to Make Games, you’ll build on your knowledge of If statements and use them to make complex games.
• In Chapter 10: Solving Problems with Subroutines, you’ll break down your code into simple sections that you use again and again. Then you’ll put all your code together to make a game where you battle a fire-breathing dragon!
• In Chapter 11: Event-Driven Programming, you’ll make interactive programs, such as a simple drawing program, that respond to user input.
• Chapter 12: Building Graphical User Interfaces covers how to create a full application with buttons, labels, and all the bells and whistles of a professional program. You’ll build on your simple drawing program and create buttons that let your user change the pen color.
• Chapter 13: Repeating For Loops will show you how to use For loops in your programs to avoid repeating code. You’ll learn how to automate boring tasks and draw a bunch of pictures in just a few lines of code.
• Chapter 14: Creating Conditional While Loops discusses more advanced conditional programming and ends with making a game of rock-paper-scissors you can play against the computer.
• Chapter 15: Grouping Data in One-Dimensional Arrays introduces arrays and how to store large amounts of data. Storing and manipulating data is another important aspect of programming, and you’ll take advantage of it to program a magic 8 ball.
• Chapter 16: Storing Data with Associative Arrays shows you how to store strings in descriptive arrays. You’ll transform your computer into a poet by writing a program that generates poems all on its own.
• Once you have a handle on arrays, in Chapter 17: Expanding to Higher-Dimension Arrays, you’ll take arrays up to two or more dimensions, which lets you put a lot more data in them. At the end you’ll create your own treasure-hunting game.
• Chapter 18: Advanced Text Magic teaches you how to handle and process text in your programs. Then you’ll use your knowledge to write a simple spell-check program.
• Chapter 19: Receiving File Input and Output helps you build bigger programs by teaching you how to handle files full of data. You’ll then use that knowledge to create a program featuring a math wizard.
Online Resources
Visit http://www.nostarch.com/smallbasic/ to download the extra book resources and to find updates. You’ll find these additional resources and review questions for teachers and students:
Book Programs and Solutions Download the finished programs, all the images you’ll need, some skeleton code for the Programming Challenges, and the solutions to the Programming Challenges and Try It Out exercises. This will save wear and tear on your typing fingers!
Additional Resources These are online articles that relate to the topics covered in this book. Many of these were written just to supplement the book!
Review Questions Test your knowledge (or your student’s knowledge).
Practice Exercises In addition to the Try It Out exercises and the Programming Challenges in the book, you can even find more exercises to practice. This is also great for teachers who want more options for assignments.
A Note to the Reader
When learning a new skill, there’s nothing more important than practice. Reading this book is only the first step. To become a great programmer, you must program! The more of the book’s resources you use, the more you learn. Don’t be afraid to experiment. No matter what buttons you press or what commands you give, you won’t hurt the computer. We promise.
With a little patience and dedication, you’ll soon amaze your friends with the wonderful things you’ll create. We want to empower you to make fun games and even to change the world!
1
INTRODUCING SMALL BASIC
Bill Gates once had a goal to get a computer into every home. Now, nearly every desk has a personal computer—so just about anyone can learn to code, too. In this book, you’ll learn to program with a language called Microsoft Small Basic.
We’ll start this chapter by explaining some general computing concepts and Small Basic itself. Then we’ll show you how to set up everything you need to use Small Basic and top it off with writing your first program!
What Is a Computer?
A computer is an electronic device that processes data according to a set of instructions—it’s that magical device in your pocket or on your desk or lap. Computers can perform calculations (like your math teacher) and compare numbers (like in fantasy football), and they can store, retrieve, and process data at high levels of speed and accuracy (like parents remembering a curfew).
A computer’s hardware is everything you can touch on your computer—inside the guts of every computer are hundreds of interconnected electronic pieces. If you want to imagine data inside your computer, picture a massive mall with hundreds of stores and tens of thousands of shoppers moving like clockwork between the stores.
But without something more, all that hardware couldn’t do anything useful. Every computer requires programs to tell it what to do—we call these instructions software. The people who can write software are called programmers—and you’re about to become one today.
What Is a Computer Program?
A computer program is a set of instructions given to a computer to perform a task (like a list of homework from your teacher). Your web browser, your favorite video games, word processors—these are all computer programs.
A program tells the computer what data to read (like numbers or text), where to read the data from (like from a user, file, or the Internet), how to process this data (it might search, sort, or calculate the data), what kind of information to produce (like paragraphs, reports, or graphs), where to store the produced output (like a disk, network, or database), and how to display the results (like through a monitor, printer, or plotter). Whoa, that’s a lot!
A computer program specifies every detail along the way. Computers communicate in machine language, which is a bunch of 1s and 0s. (Can you imagine talking 1s and 0s to your friends?) A long time ago, the first computer programs ever written were actually entered into to the computer by flipping some switches on the computer’s front panel (on for 1, off for 0). Would you want to flip switches all day? Imagine the errors!
Luckily, computer scientists invented programming languages, which are a lot easier to use than machine language. Today there are hundreds of programming languages, but Small Basic is the programming language you’ll learn in this book!
What Is Small Basic?
Small Basic is a free programming language that Microsoft created for anyone who wants to learn programming. You can write all kinds of applications with Small Basic, including games, simulations, animations, and more.
How did the language come about? It started with a programmer at Microsoft named Vijaye Raji. Raji had just read David Brin’s article, Why Johnny Can’t Code,
¹ which describes how valuable it is to learn and teach coding in BASIC. In his article, Brin challenged Microsoft to make a new BASIC language that would help kids learn to code, and Raji accepted that challenge. Although BASIC was crucial to Microsoft’s success in the 1970s, 1980s, and 1990s, there really wasn’t a great programming language suitable for beginners in 2007.
So Raji wondered if he could create a smaller version of BASIC using only the simplest parts of the original language. On October 23, 2008, he released Microsoft Small Basic v0.1, the first version of Small Basic.
The Vision of Small Basic
Small Basic’s four goals will help make your learning experience as awesome as possible:
• It’s Simple. Small Basic is a simple programming language with a helpful code Editor and a Help Area to make coding easy.
• It’s Fun. Small Basic lets you create games and other cool programs right away. It also lets you command a turtle to make art, and it’s fun to use!
• It’s Social. With Small Basic, you can publish your game to the Web on the Microsoft gallery, show it to your friends, and embed it on your blog or website. Your friends can import your program and collaborate with you to make it better.
• It’s Gradual. Once you learn the fundamentals of programming with Small Basic, it’s easy to export your code into the free Visual Studio Community and start a new adventure with Visual Basic .NET, a programming language that’s used by millions of professional programmers and an important next step in your learning journey.
We’ll cover everything you need to get started with Small Basic in this book!
The Basics of Small Basic
The three main parts of Small Basic are the language, the supporting library, and the programming environment, which is the interface you’ll use to write your own programs. Let’s explore each element now.
The Small Basic Language
To form a valid sentence in English, you need to follow its grammatical rules. In the same way, to write a valid Small Basic program, you must follow the grammatical rules of Small Basic, which are called syntax rules. Syntax includes punctuation, spelling, statement ordering, and so on. When you break these rules, Small Basic detects all the syntax errors in your program and reports them to you so you can fix them.
The Small Basic Library
The Small Basic library