- •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
134 Part II: Learning Programming with Liberty BASIC
Looping a Fixed Number of Times
A conditional loop, such as the WHILE-WEND loop, stops repeating itself only if a certain condition becomes true or false. So the number of times that a loop repeats can vary from one moment to the next.
Sometimes, however, you may need to loop a specific number of times. Although you can still use a conditional loop, you may prefer to use another type of loop known as the FOR-NEXT loop, which in Liberty BASIC looks as follows:
FOR counter = start TO end ‘ One or more instructions NEXT
This loop tells the computer to repeat a fixed number of times the instructions between the FOR and NEXT commands. The start and end values determine the number of repeats. A typical FOR-NEXT loop looks as follows:
FOR I = 1 TO 10
PRINT “The square of “; I; “ is “; I * I
NEXT
END
If you run this program, here’s what happens:
1.The first line creates the variable I and tells the computer to keep repeating the command 10 times.
2.The second line tells the computer to print The square of 1 is 1. Each time that this line runs, the value of I is different, so the final output from the program looks as follows:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100
3.The third line tells the computer to go back to the second line.
4.The fourth line tells the computer that the program is at an end.
Chapter 10: Repeating Yourself with Loops 135
If the start value is less than the end value, the FOR-NEXT loop doesn’t run at all, as in the following example:
FOR counter = 8 TO 2
‘ This doesn’t work at all NEXT
Counting with different numbers
Most FOR-NEXT loops count from 1 to another fixed value, such as 10. The FOR-NEXT, however, loop can count from any number to any other number, as in the following example:
FOR I = 8 TO 14
PRINT “The value of I =”; I
NEXT
END
This loop repeats seven times and prints the following:
The value of I = 8
The value of I = 9
The value of I = 10
The value of I = 11
The value of I = 12
The value of I = 13
The value of I = 14
You can also use negative numbers for the start and end values, as follows:
FOR counter = -5 TO 3
‘ One or more instructions NEXT
Unless you have a good reason for choosing different numbers to start and end, a good idea is to always start at number 1. Using 1 as the starting point simply makes determining how many times the FOR-NEXT loop repeats easier for you or someone else.
Counting in increments
A FOR-NEXT loop counts by one. In the following example, the loop runs four times:
FOR counter = 1 TO 4
‘ One or more instructions NEXT
136 Part II: Learning Programming with Liberty BASIC
If you like, you can use the following STEP command to make the FOR-NEXT loop count in any increments other than one:
FOR counter = 1 TO 4 STEP increment ‘ One or more instructions
NEXT
If, for example, you want to count by twos, you can use the following FOR-
NEXT loop:
FOR I = 1 TO 8 STEP 2
PRINT “The value of I = “; I
NEXT
If you run this program, the FOR-NEXT loop doesn’t repeat eight times, but rather four times and prints this:
The value of I = 1
The value of I = 3
The value of I = 5
The value of I = 7
You can make a FOR-NEXT loop count backward by using a negative number for the increment value, which is the only time that you can have the start value greater than the end value, as shown in the following example:
FOR I = 6 TO -8 STEP -3
PRINT “The value of I = “; I
NEXT
If you run this program, the FOR-NEXT loop repeats five times, printing the following results:
The value of I = 6
The value of I = 3
The value of I = 0
The value of I = -3
The value of I = -6
Chapter 10: Repeating Yourself with Loops 137
How the FOR-NEXT loop looks in a C program
Because many people eventually graduate to programming in C, here’s a glimpse at how a FOR-NEXT loop looks in a C program:
main ()
{
int counter;
for (counter = 1; counter <= 5; counter++) {
printf (“The square of %d is %d\n”, counter, counter * counter);
}
}
This FOR-NEXT loop tells the computer, “Create an integer variable, counter, and set its value to 1 (counter = 1). Then keep incrementing the value of counter by 1 (counter++) until the Boolean expression (counter <= 5) becomes false. Then stop looping.” (This FOR-NEXT loop repeats itself exactly five times.)
If the C version of the FOR-NEXT loop looks a bit cryptic, that’s because it is. Now you know why learning the fundamentals of programming is much easier if you learn BASIC rather than C.
Exiting a FOR-NEXT loop prematurely
The FOR-NEXT loop repeats a fixed number of times. However, there may be times when you want to exit out of the FOR-NEXT prematurely. To do this, you can use the magic EXIT FOR command as follows:
FOR I = 1 to 10
PRINT “This is I = “; I
IF I = 5 THEN EXIT FOR
NEXT
END
Normally this FOR-NEXT loop would repeat ten (10) times but the IF I = 5 THEN EXIT FOR command tells the program to stop after the program has looped only five (5) times.
138 Part II: Learning Programming with Liberty BASIC
Part III
Advanced Programming with Liberty BASIC
In this part . . .
In this part you find all the fancy things you can do with Liberty BASIC — create graphics, make sounds, save data on a floppy disk or hard drive, create a real Windows user interface, and much more. After you understand these more advanced features of Liberty BASIC, you can create useful and interesting programs that rival the features that
you find in commercial-quality software.
To maximize your programming skills, make sure that you type and run the many Liberty BASIC programs that sprinkled throughout each chapter. And keep your eyes open for all the different ways to write the same program in both Liberty BASIC and other languages such as C/C++, Pascal, or Java. The more that you use Liberty BASIC to help you master general programming principles now, the easier you can migrate to another language such as C/C++ later.
Chapter 11
Writing Large Programs
by Using Subprograms
In This Chapter
Understanding structured programming
Dividing a program into modules
Creating subroutines
Creating functions
Programming is not so much a hard science as it is a creative art form. The ultimate goal of programming is to write the smallest possible pro-
gram that uses the least amount of computer resources (memory, hard drive space, and so on) while accomplishing as much as possible.
Although some people are naturally talented at writing small, tight, fast code (to use the lingo of the programming community), most people need guidelines to help them write programs.
Small programs are easy to write and read. But if you’re writing a large program that consists of several thousand lines of instructions, guess what? You’d better organize your program carefully from the start, or you may waste lots of time writing a large program that doesn’t work.
Breaking the Bad Programming
Habits of the Past
In the old days, people wrote programs without any advance planning, which is like trying to write a novel by sitting down at a word processor and typing continuously until you’re done. You can possibly write a decent novel that way, but you’re more likely to create an unreadable mess.
142 Part III: Advanced Programming with Liberty BASIC
Similarly, programmers used to write programs by typing commands into the computer just to get something to work. After they had a simple program that worked, they started adding new instructions into the program.
Unfortunately, programmers often didn’t add new instructions with any planning or organization. Some programmers added new instructions in the beginning of a program; others put new instructions at the end; and still others sprinkled new instructions throughout the program’s existing instructions so that determining where the new instructions began and the old ones ended was nearly impossible.
One reason programmers used to write programs that were hard to read is because they could use a special programming command, the GOTO command. The GOTO command tells the computer to jump to another part of the program. GOTO LabelOne, for example, tells the computer to suddenly jump to the part of the program that LabelOne identifies.
The GOTO command encouraged programmers to write programs that told the computer to jump from one place in the program to another to find instructions. Programs that use the GOTO command are like a novel in which page 2 tells you to jump to page 349, page 349 tells you to jump to page 34, page 34 tells you to jump to page 125, and so on.
Because bouncing from one part of a program to another can prove as confusing as trying to untangle strands of spaghetti, writing programs in this manner became known as spaghetti coding.
Consider, for example, the following Liberty BASIC program:
GOTO [LabelOne] [LabelFour]
PROMPT “How much money do you have”; MyCash GOTO [LabelThree]
[LabelTwo] END [LabelOne]
GOTO [LabelFour] [LabelThree]
PRINT “You owe me = “; MyCash * .95 GOTO [LabelTwo]
Trying to figure out what this program does is confusing because the instructions are jumbled. But the following breakdown shows you how the program works:
Chapter 11: Writing Large Programs by Using Subprograms 143
1.The first line tells the computer to jump to the part of the program that the label [LabelOne] (which happens to be on the seventh line of the program) identifies.
2.The computer jumps to the seventh line in the program, which contains the label [LabelOne].
3.The eighth line in the program tells the computer to jump to the part of the program that the label [LabelFour] (which happens to be the second line of the program) identifies.
4.The computer jumps to the second line in the program, which contains the label [LabelFour].
5.The third line displays a Prompt dialog box that asks, How much money do you have? on-screen and waits for the user to type a number, which the program stores in the variable MyCash.
6.The fourth line tells the computer to jump to the part of the program that the label [LabelThree] (which happens to be the ninth line in the program) identifies.
7.The computer jumps to the ninth line in the program, which contains the label [LabelThree].
8.The tenth line prints, You owe me = and follows it with the value of MyCash multiplied by .95.
9.The eleventh line tells the computer to jump to the part of the program that the label [LabelTwo] (which happens to be the fifth line of the program) identifies.
10.The computer jumps to the fifth line of the program, which contains the label [LabelTwo].
11.The sixth line tells the computer that the program is at an end.
The preceding GOTO program is equivalent to the following simple program:
PROMPT “How much money do you have:”; MyCash
PRINT “You owe me =”; MyCash * .95
END
In the early days of computer programming, structures such as IF THEN and SELECT CASE statements didn’t exist. (See Chapter 9 for more information about IF THEN statements.) For many years, the GOTO command was the only way programmers had to tell the computer to skip over certain instructions or follow a different set of instructions. Although you can still use the GOTO command, you should avoid using GOTO commands whenever possible.