- •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
50 |
Part I: Programming a Computer |
Using a Compiler or an Interpreter
After you type your instructions in an editor by using a programming language such as C++ or Java, guess what? The computer doesn’t have the slightest idea what you just created. Computers understand only machine language, so you need to use another special program to convert your source code (the instructions that you write in C++ or Java) into machine language.
You can use either of the following two types of programs to convert source code into machine language:
A compiler
An interpreter
Compilers
A compiler takes your source code, converts the entire thing into machine language, and then stores these equivalent machine language instructions in a separate file, often known as an executable file. The process is like having a translator study an entire novel written in Spanish and then translate it into Arabic.
Whenever a compiler converts source code into machine language, it’s compiling a program.
After you compile a program, you can just give away copies of the executable (machine-language) version of your program without giving away your source code version. As a result, most commercial programs (such as Microsoft PowerPoint and Quicken) are compiled.
After you use a compiler to convert source code into machine language, you never need to use the compiler again (unless you make changes to your source code).
A compiler creates machine language for a specific microprocessor, such as the PowerPC (which the Macintosh uses) or the Intel Pentium family of microprocessors (including clone microprocessors, such as the AMD Athlon). If you write a program in BASIC and want to run it on a Macintosh and a Windows computer, you need to compile your program twice: once for the Macintosh and once for the Windows environment.
Not all compilers are equal, although two different compilers may convert the same language into machine language. Given identical C++ source code, for example, one C++ compiler may create a program that runs quickly, whereas a second C++ compiler may create a smaller file that runs much slower.
Chapter 4: The Tools of a Computer Programmer |
51 |
Interpreters
A second, but less popular, way to convert source code into machine language is to use an interpreter. An interpreter converts each line of your source code into machine language, one line at a time. The process is like giving a speech in English and having someone translate your sentences, one at a time, into another language (such as French).
Unlike what a compiler does, an interpreter converts source code into machine language but stores the machine-language instructions in the computer’s memory. Every time that you turn off the computer, you lose the machine-language version of your program. To run the program again, you must feed the source code into the interpreter again.
If anyone wants to run your program, that person needs both an interpreter and the source code for your program. Because your source code enables everyone to see how you wrote your program (and gives others a chance to copy or modify your program without your permission), very few commercial programs use an interpreter.
Most Web-page programming languages use interpreters, such as JavaScript and VBScript. Because different computers can view Web pages, you can’t compile programs that you write in JavaScript or VBScript into machine language. Instead, your computer’s browser uses an interpreter to run a JavaScript or VBScript program.
In the old days, when computers were slow and lacking in sufficient memory and storage space, interpreters were popular because they gave you instant feedback. The moment you typed an instruction into the computer, the interpreter told you whether that instruction would work and even showed you the results. With an interpreter, you could write and test your program at the same time. Now, computers are so fast that programmers find using a compiler easier than using an interpreter.
P-code: A combination compiler and interpreter
Getting a program to run on different types of computers is often a big pain in the neck. Both Macintosh and Windows programs, for example, use pull-down menus and dialog boxes. You need to write one set of commands to create pull-down menus on the Macintosh, however, and a second set of commands to create the identical menus in Windows.
52 |
Part I: Programming a Computer |
Because one program almost never runs on multiple computers without extensive modification, programmers combined the features of a compiler with an interpreter to create something called p-code.
Instead of compiling source code directly into machine language, p-code compiles source code into a special intermediate file format. To run a program compiled into p-code, you use an interpreter. This two-step process means that after you compile your program into p-code, you can run your p-code interpreted program on any computer that has the right p-code interpreter.
Java is the most popular programming language that uses p-code. After you compile a Java program into p-code, you can copy that p-code to a Macintosh, a Windows computer, or a Linux computer. As long as that computer uses a Java p-code interpreter, you can run the Java program on that computer without modification.
Best of all, programs that you compile into p-code can run without the original source code, which means that you can protect your source code and still give your program away to others.
Just in case you’re wondering, Liberty BASIC, which comes with this book, takes BASIC instructions and saves them in a separate file that uses p-code. If you distribute any compiled programs that you create using Liberty BASIC, you need to also distribute a special run-time file that can translate your Liberty BASIC p-code on another computer.
Naturally, p-code has its own disadvantages. Programs that you create by using p-code tend to run much slower than programs that you compile directly into machine language. Although p-code programs can run without a copy of the original source code that you use to create them, you can also decompile p-code programs.
Decompiling a p-code program can reveal the original source code that the programmer used to create the program. So if you write a program in Java and compile it into p-code, a rival can decompile your p-code program and see your original Java source code. Your rival then ends up with a nearly identical copy of your source code, essentially stealing your program.
You can actually decompile any program, including programs that you compile into machine language. But unlike with decompiling p-code programs, decompiling a machine-language version of a program never gets you the original high-level language source code that the programmer used to write the program. If you compile a program into machine language, the original source code can be written in C++, COBOL, FORTRAN, BASIC, Ada, LISP, Pascal, or any other programming language in the world. Because the decompiler has no idea what language the original source code was written in, it can only decompile a machine-language version of a program into equivalent assembly