- •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
144 Part III: Advanced Programming with Liberty BASIC
Introducing Structured Programming
You never find one “right” way to write a program, but programmers have created different ways to write programs that are at least well organized. One popular way to write a program is known as structured programming, and its main idea is to organize your program by using only three types of instructions (none of which resembles the GOTO command). If you use only the following three instructions, you ensure that you and other people can easily read and understand your program:
Sequential instructions
Branching instructions
Looping instructions
The following sections describe each of these types of instructions.
Sequential instructions
The simplest way to organize instructions in a program is to place them sequentially, or one after another, as in the following example:
PROMPT “How much stuff did you steal last year”; Amount
TaxesOwed = Amount * .95
PRINT “This is how much tax you owe =”; TaxesOwed
END
Unfortunately, you can’t write every program as one big list of instructions. If the computer needs to make a decision, your program may need to choose between two or more different sets of instructions. Programs that must make a choice are said to branch.
Branching instructions
Branching instructions (such as the IF THEN statement) provide two or more different instructions for the computer to follow, based on a certain condition. (For more information about IF THEN statements and other types of branching statements, see Chapter 9.) The following program, for example, calculates two different taxes owed, depending on whether you’re a politician:
Chapter 11: Writing Large Programs by Using Subprograms 145
PROMPT “How much stuff did you steal last year”; Amount TaxesOwed = Amount * .95
PROMPT “Are you a professional criminal (Y or N)”; Answer$ IF (Answer$ = “N”) THEN
PRINT “This is how much tax you owe =”; TaxesOwed ELSE
PRINT “Lawyers and politicians don’t need to pay taxes.” END
Branching instructions offer two or more alternative sets of instructions for the computer to follow. As a result, branching instructions are harder to read than instructions that you organize sequentially because you must determine which set of instructions the computer may follow at any given time.
Looping instructions
Sometimes the computer may need to repeat certain instructions. Rather than type the same instructions over and over, you can use a loop, such as a FOR-NEXT or a WHILE-WEND loop.
A FOR-NEXT loop repeats a fixed number of times. A WHILE-WEND loop repeats itself while a certain condition remains true. Thus the number of times that a WHILE-WEND loop repeats itself can range from zero to infinity. (See Chapter 10 for more information about looping.)
The following program, for example, asks for a password, checks to see whether the user types the correct password (which is the string “open”), and repeats these instructions until the user types the correct password:
PROMPT “What is the password”; Password$
WHILE Password$ <> “open”
PRINT “Wrong password, moron. Try again.”
PROMPT “What is the password”; Password$
WEND
PRINT “You typed the correct password!”
END
Loops can prove harder to read than sequential instructions and branching instructions because you can’t always tell how many times a loop repeats itself. Essentially, a loop is a shortcut so that you don’t need to type a long series of sequential instructions in your program. (For more information about loops, see Chapter 10.)
146 Part III: Advanced Programming with Liberty BASIC
Putting structured programming into practice
The reason for organizing your program in chunks of sequential, branching, and looping instructions is to make how your program works easier for others to understand. If they can understand how your program works, they can modify and improve on it later.
Just because you write a program, don’t be so sure that you can understand it later. If you write a program consisting of several thousand lines of instructions, you’re likely to forget how certain parts of the program work — especially if you put the program aside and work on another project for awhile. So writing programs that are easy to understand is crucial for your own benefit and for the benefit of any other programmer whose job is to fix or modify programs that you write.
To see how structured programming can make a program easier to read, look at the following program, which consists of sequential, branching, and looping instructions:
‘Sequential instructions
PRINT “This program prints a message, of your” PRINT “choosing, on the screen.”
PROMPT “What message do you want to appear”; Message$ PROMPT “Display message in all UPPERCASE (type U) or lowercase (type l)?”; WhatCase$
‘Branching instructions IF WhatCase$ = “U” THEN
Message$ = UPPER$(Message$)
END IF
IF WhatCase$ = “l” THEN
Message$ = LOWER$(Message$)
END IF
‘Looping instructions FOR I = 1 TO 15
PRINT SPACE$(I + 4); Message$ NEXT
END
Think of sequential, branching, or looping instructions as the building blocks of any program. If you write a program that uses only sequential, branching, and looping instructions, your program will be easier for you (or anyone else) to read and edit at a later date.