- •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
24 |
Part I: Programming a Computer |
Although, in theory, C programs can run on different computers without modification, the reality is that you almost always must modify a C program slightly or drastically to get it to run on a different computer. Modifying a C program, however, is still much easier than modifying an assembly or machine-language program.
Given its power and portability, C has quickly become one of the most popular programming languages in the world. The majority of all programs are written in C although most newer programs are now written in a C derivative language called C++. Some of the more famous (or infamous) programs that have been written in C or C++ include operating systems such as Windows 95/98/Me/NT/2000/XP, Unix, and Linux, as well as major commercial programs such as Quicken, Netscape Navigator, and Microsoft Word.
Although C is popular, it has its share of flaws:
C creates larger and slower programs than equivalent assembly or machine-language programs.
The C language gives programmers access to all parts of a computer, including the capability to manipulate the computer’s memory. Unfortunately, all this power can prove as dangerous as giving a hyperactive monkey a chainsaw and a hand grenade. If you don’t write your C programs carefully, they can accidentally wreck your computer’s memory, causing your program to crash your computer.
In a desperate attempt to make C programming more reliable, programmers developed languages similar to C, such as C++, Java, Perl, Python, and C#. All of these C-derived languages add a special feature known as object-orientation, which encourages programmers to write small programs that they can easily reuse and modify. In addition, these other languages try to protect programmers from writing programs that can mess up the computer’s memory, as C programs can do, which decreases the chance of writing a program that crashes an entire computer.
High-level programming languages
Because writing machineor assembly-language programs was so difficult and confusing, people developed additional languages that look more like human languages, with names such as FORTRAN, COBOL, BASIC, Pascal, and Ada. By making programming languages look more like ordinary human languages, the creators of these high-level languages hoped to make programs easier to write and modify later on.
One of the first high-level languages was FORTRAN (which stands for FORmula TRANslator). FORTRAN was designed specifically for mathematical calculations. Another early high-level language was COBOL (COmmon Business- Oriented Language), which was designed for business data processing.
Chapter 2: All about Programming Languages |
25 |
Because each language has a specialized purpose, most people aren’t going to use FORTRAN or COBOL to write video games, operating systems, or word processors (although you can still do so if you really want).
Because programming was still too difficult for many people, computer scientists soon created both Pascal and BASIC to teach people programming. BASIC — Beginner’s All-purpose Symbolic Instruction Code — was designed to teach complete novices how to program. Beginners could start learning to program by using C, but the complexities of C can discourage people too soon — sort of like trying to teach a three-year-old how to ride a bicycle by putting him on a motorcycle in the middle of rush-hour traffic.
The main advantage of BASIC is its simplicity. To print the words “Take a nap!” on-screen, you need only the one following command:
PRINT “Take a nap!”
If you compare it with the equivalent C or assembly language source code, BASIC source code enables you to focus on the task that you want to accomplish instead of worrying about the cryptic commands of a specific programming language.
Pascal (named after the French philosopher, Blaise Pascal) is another language designed to help beginners learn how to program. The main difference between BASIC and Pascal is that Pascal encourages you to write wellstructured programs that you can easily read, understand, and modify at
a later date. The following is a Pascal program that displays “Take a nap!” on-screen:
Program Message (Input, Output);
Begin
Writeln (‘Take a nap!’);
End.0
Compared with Pascal, BASIC is much less structured, which makes writing a BASIC program easy but makes reading and understanding large BASIC programs much more difficult. Pascal is more structured and forces you to plan your program before you write, as you do if you first plan a trip before leaving home. This planning may take longer, but your program and your trip will be more organized than if you rush into writing the program right away, which can be as disorganized as showing up in Paris in the middle of the night with no hotel reservations. On the other hand, BASIC enables you to start writing your program right away, which is more likely to lead to a more disorganized program.
BASIC is such a popular language that programmers tried to combine the structured features of Pascal with the simplicity of BASIC to create various dialects of BASIC. Liberty BASIC (see Chapter 5) is one example of a structured version of BASIC.
26 |
Part I: Programming a Computer |
As usual, high-level programming languages such as Pascal, BASIC, FORTRAN, Ada, and COBOL have their own share of problems:
High-level programming languages create larger and slower programs than equivalent C, assembly language, or machine-language programs.
High-level programming languages shield you from accessing all the parts of the computer, preventing you from using the power available in C, assembly language, or machine-language programs. As a result, writing certain types of programs, such as operating systems or disk utility programs (such as the Norton Utilities), is more difficult (but not impossible) in high-level languages.
High-level programming languages more closely resemble human languages, so writing a compiler for a high-level language is more difficult. If your computer doesn’t have a compiler for your favorite high-level language (such as Ada), you can’t write a program for your computer in that particular programming language.
Of course, nobody would use high-level programming languages such as Pascal, BASIC, FORTRAN, Ada, and COBOL unless they offered some advantages over C, assembly language, or machine language. Following are several reasons for using a high-level programming language:
You can write programs much faster in a high-level programming language than you can in assembly language or machine language. (You can write a program in C in roughly the same amount of time as in a highlevel language such as Pascal.)
Learning and mastering a high-level programming language takes less time than learning and mastering machine language, assembly language, or C programming.
Because high-level programming languages shield you from accessing all parts of a computer, they protect you from writing programs that accidentally mess up the computer, causing it to crash.
Reading and modifying a program written in a high-level language is much easier than reading and modifying an equivalent program written in C, assembly language, or machine language.
Programs written in high-level languages can run on a variety of computers. If you write a program in a high-level language, you can (theoretically) port that program to run on a different computer.
Naturally, high-level languages have their own share of problems, which include the following:
Programs written in high-level languages are more complicated to translate into machine code, which means that a program written in Ada or COBOL will likely run much slower and require more memory and disk space than a similar program written in C, assembly, or machine language
Chapter 2: All about Programming Languages |
27 |
Creating a compiler for a high-level language is much harder than creating one for C. As a result, not all computers have compilers available for every high-level language. So if you write a program in Modula-2 or LISP, you may find it nearly impossible to port to another computer or operating system.
Rapid Application Development (RAD) programming languages
Most programming languages were designed back in the days when computer screens displayed nothing but text. The screen didn’t show graphics, mouse pointers, buttons, or windows.
Because computer screens could display only text, languages such as C++, BASIC, and Pascal just had simple commands to display information, such as the following BASIC command:
PRINT “This sentence appears on-screen.”
After computers developed fancy graphical user interfaces with windows, scroll bars, and toolbars, people began demanding programs that included all these fancy graphical features as well. To help programmers create programs with fancy user interfaces, many companies developed special dialects of existing languages, which they dubbed rapid application development (RAD) languages.
RAD languages enable programmers to design the way that they want their program to look (the user interface) and then write source code to make that user interface actually do something useful, such as display information in a window. Figure 2-1 shows such an interface in Real Basic.
Some popular RAD languages are Visual Basic and Real Basic (based on BASIC), Delphi and Kylix (based on Pascal), Visual C# (based on C++), and JBuilder (based on Java).
RAD languages offer the following benefits:
You can write programs with graphical user interfaces much faster by using RAD than you can by using ordinary C++, BASIC, or Pascal. Figure 2-2 shows StoryCraft, a story-creating program that two profes-
sional fiction writers developed to help people create original stories for novels, short stories, plays, or screenplays.
28 |
Part I: Programming a Computer |
Figure 2-1:
Real Basic enables you to create a user interface and then
write BASIC commands to make that user interface do something useful.
Figure 2-2:
StoryCraft was written in Visual Basic to help writers create better stories, novels, and screenplays.