- •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
90 |
Part II: Learning Programming with Liberty BASIC |
Defining the type of data that variables can hold
In addition to enabling you to create and name your variables at the beginning of a program, most programming languages (but not Liberty BASIC) also force you to define the type of data that each variable can hold. Defining your data types serves the following two purposes:
It identifies the type of data that each variable can hold. If you clearly identify what type of data a variable can hold, you (or another programmer) can better understand where a program may store data and what type of data it can store in each specific variable.
It prevents bugs by keeping variables from storing the wrong types of data by mistake.
Just so that you can see how other programming languages declare variables and the data types that they can hold, the following example shows
how the C programming language declares a variable by the name of IQ to hold an integer value:
main ()
{
int IQ
}
In the Pascal programming language, that same variable declaration may look as follows:
Program Main;
Var
IQ : integer;
End.
And in certain BASIC dialects, such as Visual Basic, that same variable declaration looks like the following example:
DIM IQ AS INTEGER
Declaring variables isn’t just for the convenience of the computer; it’s for the convenience of the programmer who must read, understand, and modify a program later.
Using Constants
The value stored in a variable can be changed while the program’s running; that’s why they’re called variables (because their values can vary). Sometimes, however, you may want to use a fixed value throughout your program. Look, for example, at the following program. Can you figure out what the number .1975 stands for?
Balance = 43090
OurProfit = Balance * .1975
Balance = Balance + OurProfit
PRINT “Pay this amount, or we’ll get you = “; Balance
PRINT “Today’s current loan sharking interest rate = “; .1975 END
Chapter 7: Variables, Constants, and Comments |
91 |
A quick glance at the preceding program shows that the meaning of the number .1975 isn’t obvious. Because the meaning of numbers isn’t always clear without additional explanation, programmers use constants. By using a constant, you can use a descriptive name to represent a fixed value.
Liberty BASIC supports an older version of the BASIC programming language that doesn’t support constants. However, other versions of BASIC (such as Visual Basic and most other programming languages) do support constants.
To see how constants work, study the following program written in the Pascal programming language:
Program UnderstandingConstants; Const
InterestRate = 0.1975; Var
OurProfit : real; Balance : real;
Begin
Balance := 43090;
OurProfit := Balance * InterestRate; Balance := Balance + OurProfit;
Writeln (‘Pay this amount or we’ll get you! = ‘, Balance:6:2);
Writeln (‘Today’s current loan sharking interest rate = ‘, InterestRate:1:4);
End.
If you run this program, you’ll see the following output:
Pay this amount or we’ll get you = 51600.28
Today’s current loan sharking rate = 0.1975
As you can see in the above program, the value of 0.1975 is used twice in the program (on lines 9 and 12). If the interest rate changed from 0.1975 to 0.2486 and your program did not use constants, you would have to exhaustively search through your entire program and change every line that uses the value of 0.1975 to 0.2486. For short programs like the above program, this is tolerable. In huge programs, however, this would be time-consuming and error-prone.
So to save time and ensure accuracy, programmers use constants. By using constants in the above Pascal programming example, you only need to change the value of the constant InterestRate (in line 3) from 0.1975 to 0.2486. The computer automatically inserts this new value in lines 9 and 12, which also contain the constant InterestRate.
92 |
Part II: Learning Programming with Liberty BASIC |
Constants have the following two key advantages:
They identify numeric or string values with a descriptive name.
Changing the value of a constant automatically modifies your entire program quickly and accurately.
Commenting Your Code
If you write a small program, anyone can readily understand how it works by following it line-by-line. But if you write a large program, understanding what the program does can prove difficult for others (and even for you) without spending a long time studying each line.
To make understanding (and ultimately maintaining) a program easier (because programmers are a notoriously lazy bunch), every programming language enables you to insert comments into your source code. Comments enable you to store directly in your source code explanations to identify the following information:
Who wrote the program
The creation and last modification dates of the program
What the program does
How the program works
Where the program gets, saves, and outputs data
Any known problems with the program
In Liberty BASIC, you can add comments in one of the following two ways:
By using the REM (short for REMark) statement.
By using the apostrophe (‘).
The following program shows how to use both the REM statement and the apostrophe to insert comments into a program. Although you can use both types of comments in a single program, you want to use only one or the other for consistency’s sake.
‘Created on March 29, 2005
‘Written by John Doe
‘This program displays a not-so-subtle
‘message to potential copycats to
Chapter 7: Variables, Constants, and Comments |
93 |
‘come up with their own ideas rather
‘than steal mine.
REM This program does nothing more than
REM print a message on-screen to
REM insult any potential authors browsing
REM through this book in hopes of stealing
REM ideas to use in a competing book.
NOMAINWIN ‘ Keeps the main window from appearing
NOTICE “Don’t steal ideas from this book!”
END ‘ This last line ends the program
Because comments are for the benefit of humans only, the computer looks at the preceding Liberty BASIC program as follows:
NOMAINWIN
NOTICE “Don’t steal ideas from this book!”
END
The apostrophe is more versatile than the REM statement for making a comment because the apostrophe can create a comment that appears as a separate line or as part of an existing line. The REM statement can create only a comment that appears on a separate line.
Comments exist solely for your benefit. The computer completely ignores any comments that you insert in a program. So make your comments useful but not too wordy; otherwise, they become more of a nuisance than an aid.
Comments can prove valuable for telling Liberty BASIC to temporarily ignore one or more lines of code. Rather than delete an entire line, test to see whether the program works, and then retype the previously deleted line; for example, you can just comment out the line, as follows:
NOMAINWIN
‘ A = SQR((B * B) + (C + C)) END
If you run this program, Liberty BASIC sees only the following:
NOMAINWIN
END
To restore the commented line, just remove the apostrophe so that Liberty BASIC sees the program as follows:
NOMAINWIN
A = SQR((B * B) + (C + C)) END
94 |
Part II: Learning Programming with Liberty BASIC |
Chapter 8
Crunching Numbers and
Playing with Strings
In This Chapter
Performing mathematical operations
Using Liberty BASIC’s built-in math functions
Pulling strings with your data
Converting strings into numbers
One of the most important parts of a computer program is its capability to manipulate any data that it receives and to spit out a useful answer
that people are willing to pay for (so that you can make money). The two types of data that your program must manipulate are numbers and words (known as strings by the programming community).
Some common number-manipulating programs include spreadsheets, accounting programs, and even video games (because they need to calculate the correct way to display jet fighters or dragons popping on-screen to enable you to mow them down with a machine gun). Common string-manipulating programs include databases (which store, sort, and rearrange such data as names), word processors, and foreign-language translation programs.
Adding, Subtracting, Dividing,
and Multiplying
The four basic ways to manipulate numbers are adding, subtracting, dividing, and multiplying. By using these four mathematical operations, you can create any type of complicated mathematical formula.
To add, subtract, divide, or multiply two numbers (or two variables that represent numbers), you use the symbols shown in Table 8-1.