- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
Beginning
Programming
FOR
DUMmIES‰
3RD EDITION
by Wallace Wang
Beginning Programming For Dummies®, 3rd Edition
Published by
Wiley Publishing, Inc.
111 River Street Hoboken, NJ 07030-5774
Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, e-mail: permcoordinator@ wiley.com.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc., and/or its affiliates in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK, THEY MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A PROFESSIONAL WHERE APPROPRIATE. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2003114793
ISBN: 0-7645-4997-9
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
3B/SU/RR/QT
About the Author
After spending his first two post-college years working for a dead-end corporation that encouraged its employees to use euphemisms to disguise the fact that they were manufacturing nuclear weapons with the potential to wipe out most forms of life on the planet, Wallace Wang decided that his life was meant to be spent doing something more exciting than existing in a corporate culture that stifled freedom and democracy while building missiles ostensibly to protect freedom and democracy. With the thought of escape in his mind, he bought one of the first IBM personal computers on the market — and quickly realized that the accompanying computer manuals were completely incomprehensible.
After deciphering the manuals and learning to master the arcane commands of the ancient MS-DOS version 1.25 operating system, Wallace decided to publish fragments of his notes in a local computer magazine for the amusement of others — and to provide an alternative source of income for his eventual departure from the mentally suffocating environment of the military-industrial complex.
After people began responding favorably to his introductory computer magazine articles, he continued writing more, eventually turning to writing full-time. For the first time, he managed to earn a living in a job that didn’t involve developing something that could blow up people who happen to live in another part of the world.
Today, the author is happily pursuing a dual career in the book publishing industry and the stand-up comedy industry. His eventual goal is to convince people that enjoying yourself while learning is all right. In the meantime, he plans to continue making fun of any idiots and morons who happen to get in his way.
Dedication
This book is dedicated to all the wonderful people I’ve met along the path of life, including . . .
Cassandra (my wife), Jordan (my son), and Bo, Scraps, Tasha, and Nuit (our cats).
Lily Carnie, the only person I know who can truly see both sides of the story.
All the friendly folks I’ve met while performing at the Riviera Comedy Club, located at the Riviera Hotel & Casino in Las Vegas: Steve Schirripa (who also appears on the HBO show, The Sopranos, which you can read about at www. hbo.com/sopranos), Don Learned, Bob Zany, Gerry Bednob, Bruce Clark, Darrell Joyce, Tony Vicich, and Kip Addotta. The next time you’re visiting Las Vegas, drop by the Riviera and watch a comedy show. Then dump some money in a slot machine on the way out to ensure that the Riviera Hotel & Casino continues making enough money to keep its comedy club open.
Patrick DeGuire, who helped me form Top Bananas (at www.topbananas.com), our company devoted to providing clean, quality stand-up comedy to the wonderful people in San Diego. Thanks must also go to Leo (the man, the myth, the legend) Fontaine, Chris (the Zooman) Clobber, and Dante (who gets excited just to see his name in a book).
Author’s Acknowledgments
If it weren’t for Matt Wagner and Bill Gladstone at Waterside Productions, I may never have written this book (and someone else may have). That’s why I don’t mind paying these guys 15 percent of the book royalties so that they can afford to buy their groceries.
Additional thanks go to Allen Wyatt (the technical reviewer) for making sure that everything in this book is accurate, and to Cassandra (my wife) for putting up with multiple computers that (from her point of view) seem to spontaneously appear and disappear from the house at random. Each time a computer disappears, a more advanced model appears that promises more speed and hard disk space but still never seems to have more speed or as much room as the previous computer model that it replaced.
A final note of thanks must go to the Chinese and Russians who’ve translated my other books, Microsoft Office For Dummies and Visual Basic For Dummies. The Chinese and Russian editions are the only foreign translations of my books ever to include my previously published references to General Dynamics as a “bomb factory.” Whether translators in other countries purposely omitted this humorous reference or whether it’s just a coincidence that only the Chinese and Russian editions included this reference is unknown.
Still, this fact alone provides an endless source of amusement to think that Chinese and Russian readers are privy to an American joking about his country’s nuclear missile factories, while readers in other countries are not. For that reason alone, the Chinese and Russian translators of my books have my eternal gratitude and blessing, not because they happen to be Chinese or Russian, but because they appear to be able to appreciate a joke.
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and |
Production |
|
Media Development |
Project Coordinator: Erin Smith |
|
Project Editor: Andrea C. Boucher |
||
Layout and Graphics: Seth Conley, |
||
|
||
(Previous Edition: Linda Morris) |
Michael Kruzil, Barry Offringa, |
|
Acquisitions Editor: Bob Woerner |
Lynsey Osborne, Heather Ryan, |
|
Shae Wilson, Melanie Wolven |
||
Technical Editor: Allen Wyatt |
||
Proofreaders: TECHBOOKS Production |
||
Editorial Manager: Carol Sheehan |
||
Services, Carl William Pierce, Brian H. Walls |
||
Permissions Editor: Carmen Krikorian, |
Indexer: TECHBOOKS Production Service |
|
Senior Permissions Editor |
|
|
Media Development Specialist: Travis Silvers |
|
|
Media Development Manager: Laura VanWinkle |
|
|
Media Development Supervisor: |
|
|
Richard Graves |
|
|
Editorial Assistant: Amanda Foxworth |
|
|
Cartoons: Rich Tennant (www.the5thwave.com) |
|
|
|
|
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services