- •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
42 |
Part I: Programming a Computer |
Using multiple programming languages
Instead of writing an entire program using one programming language (such as C++), some compilers can convert source code into a special file known as an object file. The purpose of object files is that one programmer can write a program in C++, another in assembly language, and still a third in Pascal. Each programmer writes his portion of the program in his favorite language and stores it in a separate object file. Then the programmers connect (or link) all these object files together to create one big program. The program that converts multiple object files into an executable program is known as a linker.
In the world of Microsoft Windows, another way to write a program using multiple languages is to use dynamic link libraries (DLLs), which are special programs that don’t have a user interface. One programmer can use C, another can use Java, and a third can use COBOL to create three separate DLL files. Then a fourth programmer can write a program using another language such as Visual Basic, which
creates the user interface and uses the commands that each separate DLL file stores.
A third way to write a program is to use your favorite language (such as Pascal) and then write assembly language instructions directly in parts of your program. (Just be aware that not all compilers enable you to switch between different languages within the same program.)
Finally, Microsoft offers a programming framework dubbed .NET. By using the .NET framework, one programmer can program in C#, another can program in FORTRAN, and still another can program in BASIC. Then their different programs can share data and communicate with other programs through the .NET framework and create a single user interface that unifies these separate programs. The whole point to all of these different methods is that by using different programming languages, you can take advantage of each language’s strengths, while minimizing its weaknesses.
Many programmers use Visual Basic because it’s easy for creating prototypes quickly. After you use Visual Basic to create a prototype that shows how your user interface works, you can start adding actual commands to later turn your prototype into an honest-to-goodness working program.
Choosing a programming language
After you refine your prototype until it shows you exactly how your program is to look and act, the next step is choosing a programming language to use.
You can write any program by using any programming language. The trick is that some languages make writing certain types of programs easier.
The choice of a programming language to use can pit people against one another in much the same way that religion and politics do. Although you can’t find a single “perfect” programming language to use for all occasions, you may want to consider a variety of programming languages. Ultimately, no one cares what language you use as long as your program works.
Chapter 3: How to Write a Program 43
Defining how the program should work
After choosing a specific programming language, don’t start typing commands into your computer just yet. Just as programmers create mockups (prototypes) of their program’s user interface, they often create mock-up instructions that describe exactly how a program works. These mock-up instructions are known as pseudocode.
If you need to write a program that guides a nuclear missile to another city to wipe out all signs of life within a 100-mile radius, your pseudocode may look as follows:
1.Get the target’s coordinates.
2.Get the missile’s current coordinates.
3.Calculate a trajectory so the missile hits the target.
4.Detonate the nuclear warhead.
By using pseudocode, you can detect flaws in your logic before you start writing your program — places where the logic behind your program gets buried beneath the complexity of a specific programming language’s syntax.
In the preceding example, you can see that each pseudocode instruction needs further refining before you can start writing your program. You can’t just tell a computer, “Get the target’s coordinates” because the computer wants to know, “Exactly how do I get the target’s coordinates?” So rewriting the preceding pseudocode may look as follows:
1.Get the target’s coordinates.
a.Have a missile technician type the target coordinates.
b.Make sure that the target coordinates are valid.
c.Store the target coordinates in memory.
2.Get the missile’s current coordinates.
3.Calculate a trajectory so the missile hits the target.
4.Detonate the nuclear warhead.
You can refine the instructions even further to specify how the computer works in more detail, as follows:
1.Get the target’s coordinates.
a.Have a missile technician type the target coordinates.
b.Make sure that the target coordinates are valid.
1)Make sure that the target coordinates are complete.
(continued)