
- •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

Chapter 9: Making Decisions with Control Statements 121
The IF THEN ELSE statement is slightly different because it tells the computer to follow one set of instructions in case a condition is true and a different set of instructions if the condition is false. The IF THEN ELSE statement looks as follows:
IF (Boolean expression) THEN
‘Follow one or more instructions listed here ELSE
‘If the condition is false, then follow these
‘instructions instead
END IF
For an example of how this statement works, run the following program and see what happens:
PROMPT “How long were you in medical school”; Answer
IF (Answer > 4) THEN
PRINT “Congratulations! You should be able to”
PRINT “play a good game of golf in no time.”
ELSE
PRINT “You may not have been in medical school for”
PRINT “very long, but at least you should know”
PRINT “how to put on a white lab coat.”
END IF
END
Unlike the IF THEN statement, the IF THEN ELSE statement always forces the computer to follow one set of instructions no matter what. In this program, if the answer is greater than four, the computer prints out, Congratulations! You should be able to play a good game of golf in no time.
If the answer isn’t greater than four, the computer prints out, You may not have been in medical school for very long, but at least you should know how to put on a white lab coat.
The IF THEN ELSE statement always makes the computer follow one set of instructions. If you use the ordinary IF THEN statement, the computer may or may not follow one set of instructions.
Working with SELECT CASE Statements
Listing multiple conditions in an IF THEN ELSE statements can prove tedious and messy, as the following example shows:

122 Part II: Learning Programming with Liberty BASIC
PROMPT “How old are you”; Answer
IF (Answer = 21) THEN
PRINT “Congratulations! You may be able to rent a”
PRINT “car in some states.”
END IF
IF (Answer = 20) THEN
PRINT “You can’t rent a car, but you’re pre-approved”
PRINT “for 20 different credit cards.”
END IF
IF (Answer = 19) THEN
PRINT “You’re still officially a teenager.”
END IF
IF (Answer = 18) THEN
PRINT “You’re old enough to join the military and”
PRINT “fire an automatic rifle, but you still can’t”
PRINT “buy beer legally. Figure that one out.”
END IF
IF (Answer = 17) THEN
PRINT “You can see R-rated movies on”
PRINT “your own (but you’ve probably done that for years).”
END IF
END
As an alternative to multiple IF THEN ELSE statements, many programming languages offer a SELECT CASE statement, which looks as follows:
SELECT CASE Variable
CASE Value1
‘Follow these instructions if the Variable = Value1 CASE Value2
‘Follow these instructions if the Variable = Value2 END SELECT
The SELECT CASE statement provides different instructions depending on the value of a particular variable. If you rewrite the preceding IF THEN ELSE statement, the program looks as follows:
PROMPT “How old are you”; Answer
SELECT CASE Answer
CASE 21
PRINT “Congratulations! You may be able to rent a”
PRINT “car in some states.”
CASE 20
PRINT “You can’t rent a car, but you’re pre-approved
PRINT “for 20 different credit cards.”

Chapter 9: Making Decisions with Control Statements 123
CASE 19
PRINT “You’re still officially a teenager.” CASE 18
PRINT “You’re old enough to join the military and” PRINT “fire an automatic rifle, but you still can’t” PRINT “buy beer legally. Figure that one out.”
CASE 17
PRINT “You can see R-rated movies on”
PRINT “your own (but you’ve probably done that for years).” END SELECT
END
If the user types 21, the program prints, Congratulations! You may be able to rent a car in some states. If the user types 19, the program prints, You’re still officially a teenager. If the user types 17, the program prints, You can see R-rated movies on your own (but you’ve probably done that for years).
Of course, if the user types any value that the SELECT CASE statement doesn’t list, such as 22 or 16, the SELECT CASE statement doesn’t run any of the instructions within its structure.
To make sure that the computer follows at least one set of instructions in a SELECT CASE statement, just add a CASE ELSE command at the very end, as follows:
PROMPT “How old are you”; Answer
SELECT CASE Answer
CASE 21
PRINT “Congratulations! You may be able to rent a”
PRINT “car in some states.”
CASE 20
PRINT “You can’t rent a car, but you’re pre-approved”
PRINT “for 20 different credit cards.”
CASE 19
PRINT “You’re still officially a teenager.”
CASE 18
PRINT “You’re old enough to join the military and” PRINT “fire an automatic rifle, but you still can’t” PRINT “buy beer legally. Figure that one out.”
CASE 17
PRINT “You can see R-rated movies on”
PRINT “your own (but you’ve probably done that for years).” CASE ELSE
PRINT “This sentence prints out if the user does NOT” PRINT “type numbers 17, 18, 19, 20, or 21.”
END SELECT END

124 Part II: Learning Programming with Liberty BASIC
If you run this program and type 21, the program prints out Congratulations! You may be able to rent a car in some states. If the user types
20, the program prints out, You can’t rent a car, but you’re preapproved for 20 different credit cards. If the user types 19, the program prints out, You’re still officially a teenager. If the user types 18, the program prints out, You’re old enough to join the military and fire an automatic rifle, but you still can’t buy beer legally. Figure that one out. If the user types 17, the program prints out, You can see R-rated movies on your own (but you’ve probably done that for years). If the user types any other number (such as 54 or 97, the program prints out, This sentence prints out if the user does NOT type numbers 17, 18, 19, 20, or 21.
Checking a range of values
You often use the SELECT CASE statement to check whether a variable happens to exactly match a specific value, such as the number 21 or the string “yes”. But sometimes you may want to run a set of instructions if a variable falls within a range of values, such as any number between 3 and 18. In that case, you must list all the possible values on a single CASE statement, as in the following example:
CASE 1, 2, 3, , 4
This checks whether a variable represents the number 1, 2, 3, or 4 as shown in the following program:
PROMPT “How many water balloons do you have? “; Answer
SELECT CASE Answer
CASE 1, 2, 3, 4
NOTICE “You need more water balloons.”
CASE 5, 6, 7, 8
NOTICE “Now you need a target.”
CASE ELSE
NOTICE “What are you? A peace-loving hippie freak?”
END SELECT
END
In this example, if the user types a number from 1 to 4, the program prints, You need more water balloons. If the user types a number from 5 to 8, the program prints, Now you need a target. If the user types any number les than 1 or greater than 8, the program prints, What are you? A peaceloving hippie freak?