- •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
Chapter 4: The Tools of a Computer Programmer |
53 |
language. After you decompile a program into assembly language source code, you can rewrite or modify that source code. Decompiling effectively allows you to steal the ideas of others.
So what do I use?
If you want to write programs to sell, use a compiler, which protects your original source code. If you want to write a program to run on your Web page, you can use either an interpreter or p-code. If you want to write a program that can run on different types of computers, p-code may prove your only choice. As a safer but more cumbersome alternative, you can also use multiple compilers and modify your program to run on each different computer.
The language that you choose can determine whether you can use a compiler, an interpreter, or p-code. You often convert Java programs into p-code, for example, although you can compile them directly into machine language. On the other hand, you usually compile C++ and rarely interpret or convert it into p-code.
Squashing Bugs with a Debugger
Few computer programs work 100 percent correctly, which explains why computers crash, lose airline reservations, or just act erratically at times. Mathematically, writing a program that works 100 percent correctly every time is impossible because testing a program for all possible types of computers, hardware, and additional software that may interfere with the way your program runs is impossible.
A problem that keeps a program from working correctly is known as a bug.
In the early days, computers used mechanical relays and vacuum tubes instead of circuit boards and microprocessors. One day, the computer failed to work correctly. The scientists examined their program; it should have worked. So they next examined the computer itself and found that a moth had gotten smashed in a mechanical relay, preventing it from closing and thus keeping the computer from working correctly. From that point on, problems in computers have been known as bugs, even though real bugs are much less annoying and dangerous than computer bugs.
Because writing a program that works 100 percent correctly all the time is virtually impossible, operating systems (such as Windows XP) unavoidably contain bugs that keep them from working correctly. When you convert your source code into machine language, you must use a compiler or interpreter,
54 |
Part I: Programming a Computer |
which is another program that contains its share of bugs. Finally, your own program may contain bugs of its own. With so many places for bugs to creep in, you shouldn’t be surprised that bugs infest computers like cockroaches infest a cheap apartment complex.
Although you can do little about bugs in other people’s programs (except not buy the programs), you can reduce (but not completely eliminate) bugs in your own program by using a debugger. A debugger is a special program (which may also contain bugs) that can help you track down and wipe out bugs in programs that you write.
A debugger provides several ways to track down bugs in your program:
Stepping: The debugger runs your program, line-by-line, so that you can see exactly which line may contain the bug. This process is like rereading written instructions to get to another person’s house if you’re lost. By going over these instructions, one by one, you can find out where you made a wrong turn.
Breakpoints: Rather than force you to step through an entire program, line-by-line, breakpoints enable you to specify where you want to start examining your program line-by-line. So if you were lost, instead of rereading the instructions to get to another person’s house from start to finish, you skip those instructions that you know you followed correctly and examine only the remaining instructions that you aren’t sure that you followed correctly. Similarly, by using breakpoints, you can selectively examine only parts of your program, line-by-line, and skip over the parts that you know already work.
Watching: Watching enables you to see your program storing data in memory and to determine what that data may be. If your program stores incorrect data (such as saving a name instead of a telephone number), you know exactly where in your program the bug is occurring. Figure 4-2 shows a sample debugger at work that highlights a specific instruction in your program and displays a window that shows the value of specific data. Each time you examine a line in your program, the debugger shows you how that particular line affects the value you’re watching. As soon as you see the value change, the debugger shows you exactly which line in your program caused that change. This process is like having someone to tell you to drive 10 miles south down a certain road after turning right. The moment that you exceed 10 miles, a watchpoint alerts you so that you know exactly where you almost made a mistake and got lost.
A debugger essentially shows you exactly how a computer is going to interpret the instructions in your program. Of course, if you fix one bug, you may introduce several new ones. That’s why writing bug-free programs is impossible.
Chapter 4: The Tools of a Computer Programmer |
55 |
Figure 4-2:
One window displays the source code, and a second window shows all the values you’re watching.
Writing a Help File
Nobody has trouble using a doorknob, a toaster, or a microwave oven, but people still complain that computers and VCRs are too hard to use.
The problem with VCRs lies in the cryptic controls that aren’t easy to figure out just by looking at them. Similarly, the problem with computer programs is that programs are too complex to use at first glance. If you can make a program that’s actually easy to use, people can actually use it.
Because computer programs are still being designed for programmers by other programmers, computers still mystify the average user. To help the poor befuddled user, most programs now offer Help files.
A Help file provides instructions and explanations on-screen. Theoretically, if the user experiences trouble with the program, he can browse through the Help file, find an explanation or step-by-step instructions, and continue using the program. Figure 4-3 shows Microsoft PowerPoint , which desperately tries to guide users through its complicated maze of commands by providing a Help file and a cartoon Office Assistant.
56 |
Part I: Programming a Computer |
Figure 4-3:
Many programs provide online Help screens in a desperate attempt to give you the information that you need.
Although Help files still can’t substitute for designing a program that’s easy to use in the first place, most programs offer Help files anyway. To keep your program modern and up-to-date, include a Help file with your program.
To create a Help file, you can use a special Help file-authoring program, which simplifies the process of creating and organizing topics for your Help file, as shown in Figure 4-4.
Creating an Installation Program
After you write your program, test it, debug it, and write a Help file for it, the final step is to give or sell copies of your program to others. Although you can copy your program onto a floppy disk or CD and force buyers to manually copy your program to their hard drive, doing so can cause problems. Users may not copy all the files correctly, for example. And forcing users to manually copy your program to their hard drive may prove such a nuisance that most people don’t bother even trying to use your program.
Chapter 4: The Tools of a Computer Programmer |
57 |
Figure 4-4:
Help filecreation programs, such as Visual Help Pro, simplify the process of making an online Help file for your program.
To make copying a program to a user’s hard drive as easy as possible, most commercial programs include a special installation program. Users run this installation program, which automatically copies a program and all necessary files to the appropriate location on the user’s hard drive. By making the installation of a program practically foolproof, software publishers make sure that users install the programs correctly.
So the final step to distributing your program to others is to use a special installation program, which can smash all your program files into a single file that can automatically install itself on another computer.
Installation programs offer the following features for distributing programs to others:
File compression: Most programs are fairly large, which means that they can’t fit on a single floppy disk. Rather than force users to insert a series of floppy disks or compact discs (CDs) into their computer to install your program, an installation program smashes your files so that they can fit on as few floppy disks or compact discs as possible.
58 |
Part I: Programming a Computer |
Display graphics and play sounds: Installing a program usually takes a few minutes while the computer copies files from the floppy or CD to its hard drive. Rather than force the user to stare into space, an installation program can display advertisements or messages to make the installation process mildly interesting. Other uses for graphics include displaying an hourglass icon or a status bar that shows how far the installation is complete, such as 54 percent complete. That way, users know how much longer they need to wait.
Simplify the copying process: Most important, an installation program simplifies copying your program to the user’s hard drive, making the entire process accurate, fast, and foolproof.
The first impression that people get from your program is through its installation process, so an installation program helps give your program a professional appearance.
Of course, you need to make sure that your installation program installs your program correctly or the first impression that users get from your program will be a highly negative one.
Part II
Learning Programming with Liberty BASIC
In this part . . .
No matter what programming language you use (C/C++, BASIC, Prolog, COBOL, C#, Perl, Ada, Pascal,
Java, and so on), all computer programs tend to follow the same general principles. If you understand how to write a program by using one language (such as BASIC), you find that understanding a second or third programming language (such as C/C++ and Java) is much easier.
To give you a chance for hands-on programming, this book explains how to use Liberty BASIC, which is a real Windows compiler that enables you to write and create real Windows programs that you can sell or give away. Just load the Liberty BASIC compiler, type the sample Liberty BASIC programs into your computer, and run them to see how they work.
Occasionally in this part, you see a Liberty BASIC program along with an equivalent program in another language (such as C/C++, Pascal, or Java), so you can understand how other programming languages may work. You can look at these programs to see how other programming languages accomplish the same tasks while looking entirely different from an equivalent Liberty BASIC program.
Chapter 5
Getting Your Hands on a Real Language: Liberty BASIC
In This Chapter
Understanding why you should learn Liberty BASIC
Putting Liberty BASIC on your computer
Writing a Liberty BASIC program
Using the Liberty BASIC editor
Using the Liberty BASIC online help
Exiting Liberty BASIC
The best way to learn anything is to start practicing it. If you want to learn computer programming, you should start writing programs on your com-
puter as soon as possible.
You can learn to program by starting out with one of hundreds of programming languages designed for novices, such as Pascal, LOGO, and SmallTalk. But the most popular beginner’s programming language is still BASIC. BASIC is simple enough to help you understand the concepts behind programming yet powerful enough to enable you to create commercial-quality programs.
To help you in your quest to learn programming, this book comes with a shareware copy of Liberty BASIC. As long as you’re running Windows 95/98/ Me/NT/2000/XP, you can run and use Liberty BASIC on your computer. Most of this book provides examples in Liberty BASIC that you can type yourself.
If you have a Macintosh, you can’t run Liberty BASIC unless you buy a special Windows emulation program such as VirtualPC from Microsoft
(at www.microsoft.com). As an alternative, you can use either True Basic (www.truebasic.com) or Future Basic (www.stazsoftware.com). True Basic, Future Basic, and Liberty BASIC all use BASIC language, so the sample programs in this book designed for Liberty BASIC should run under both True Basic and Future Basic with only minor modifications.