- •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
96 |
Part II: Learning Programming with Liberty BASIC |
|
|
|
|
||
|
|
|
|
||||
|
|
|
|
|
|
||
|
|
Table 8-1 |
Mathematical Operators |
|
|||
|
|
Mathematical Operation |
Symbol to Use |
Example |
Result |
||
|
|
Addition |
+ |
|
2 + 5 |
7 |
|
|
|
|
|
|
|
|
|
|
|
Subtraction |
– |
77 |
– 43 |
34 |
|
|
|
|
|
|
|
|
|
|
|
Division |
/ (forward slash) |
20 |
/ 4 |
5 |
|
|
|
|
|
|
|
|
|
|
|
Multiplication |
* |
|
4 * 7 |
28 |
|
|
|
|
|
|
|
|
|
|
|
Exponentiation |
^ |
|
2 ^ 3 |
8 |
|
|
|
|
|
|
|
|
|
The division symbol (/) usually appears in two places on your keyboard: on the same key as the question mark (?) and on the numeric keypad. The exponentiation symbol (^) appears on the 6 key. You can also use the subtraction symbol (–) to indicate negative numbers, such as –34.5 or –90.
Although you already understand how addition, subtraction, division, and multiplication work, you may be less familiar with exponentiation.
Exponentiation simply multiplies one number by itself several times. The formula 4 ^ 3, for example, tells Liberty BASIC take the number 4 and multiply it by itself three times. So 4 ^ 3 really means 4 * 4 * 4, or 64.
Using variables
Any mathematical calculation (addition, subtraction, division, or multiplication) creates a single value, which you can store in a variable as follows:
TaxYouOwe = 12500 * 1.01
Rather than use specific numbers to create mathematical formulas (such as 12,500 * 1.01), however, you can substitute variables in the following way:
PROMPT “How much money did you make last year”; NetIncome
TaxYouOwe = NetIncome * 1.01
NOTICE “You owe this much in taxes = “; TaxYouOwe
END
To make your mathematical calculations easier to understand, always use variables or constants rather than actual numbers. In the preceding example, you can’t tell what the number 1.01 represents. Rather than use an actual number, substitute a descriptive constant in the formula, as follows:
Chapter 8: Crunching Numbers and Playing with Strings |
97 |
TaxRate = 1.01
PROMPT “How much money did you make last year”; NetIncome TaxYouOwe = NetIncome * TaxRate
NOTICE “You owe this much in taxes = “; TaxYouOwe END
If you use a constant (in this case, making TaxRate represent the number 1.01), you can quickly understand what the number 1.01 means and why to use it in the mathematical formula.
You can use variables in the following two ways in mathematical formulas:
To represent numbers that the mathematical formula uses
To store the value that the mathematical formula calculates
Be careful when naming variables. If you mistype a variable name or mix uppercase with lowercase letters, Liberty BASIC assumes that you’re creating a new variable, and it assigns a value of zero or a blank to the “new” variable. If your program isn’t working right, check to make sure that you spelled the variables correctly and used exactly the same upperand lowercase letters.
Working with precedence
Simple formulas such as NetIncome * TaxRate are easy to understand; you just multiply the values that both variables represent. You can create more powerful mathematical formulas, however, by combining addition, subtraction, division, or multiplication, as in the following example:
TaxYouOwe = PastTaxes + NetIncome * TaxRate
If the value of NetIncome is 50,000, the value of TaxRate is 1.01, and the value of PastTaxes is 2,500, the computer looks at the formula as follows:
TaxYouOwe = 2500 + 50000 * 1.01
So now the question is whether Liberty BASIC adds 2,500 to 50,000 and then multiplies the whole thing by 1.01 (in which case the answer is 53,025) or multiplies 50,000 by 1.01 first and then adds 2,500 (in which case the answer is 53,000).
Because the result of combining addition, subtraction, division, and multiplication in a single formula can confuse the living daylights out of people, programming languages create something mysterious known as precedence that
98 |
Part II: Learning Programming with Liberty BASIC |
tells the computer which mathematical operations to calculate first. Liberty BASIC calculates mathematical operators in the following order, from top (first) to bottom (last):
Exponentiation (^)
Multiplication (*) and (^); division (/)
Addition (+) and subtraction (–)
Before running the following Liberty BASIC program, try to figure out how the computer calculates a result:
MyMoney = 3 + 4 ^ 5 - 8 / 5 * 7
PRINT MyMoney
END
This Liberty BASIC program tells the computer to do the following:
1. The first line tells the computer to create the variable MyMoney.
Because the computer calculates exponential values first, Liberty BASIC calculates the value of 4 ^ 5, which is 1,024. The formula now looks as follows:
MyMoney = 3 + 1024 - 8 / 5 * 7
Next, the computer calculates all multiplication and division (/). Because multiplication and division have equal precedence, the computer starts calculating with the first multiplication or division (/) operator that it finds, moving from left to right. The computer calculates the value of 8 / 5 first (1.6) and then multiplies it by 7. So the formula now looks as follows:
MyMoney = 3 + 1024 - 11.2
Finally, the computer calculates all addition and subtraction, moving from left to right. First it calculates the value of 3 + 1,024 (which is 1,027); it then subtracts 11.2 from it. Thus the final answer looks as follows:
MyMoney = 1015.8
2.The second line tells the computer to print the value that the MyMoney variable stores, which is 1015.8.
3.The third line tells the computer that the program is at an end.
The computer always calculates operators from left to right if operators are equal in precedence, such as multiplication and division or addition and subtraction.
Chapter 8: Crunching Numbers and Playing with Strings |
99 |
Using parentheses
Trying to remember the precedence of different mathematical operators can prove confusing. Even worse is that the ordinary precedence of mathematical operators can mess up the way that you want the computer to calculate a result. Suppose, for example, that you type the following:
BigValue = 3 + 4 ^ 5
PRINT BigValue
END
With this program, the computer first calculates the exponential value of 4 ^ 5 (which is 1,024), and then it adds 3 to it, for a total of 1,027.
But what if you really want the computer to add 3 to 4 and then perform the exponential? In this case, you must use parentheses to tell the computer, “Hey, add 3 to 4 first and then calculate the exponential,” as in the following example:
BigValue = (3 + 4) ^ 5
PRINT BigValue
END
This program adds 3 and 4 to get 7, so the formula becomes BigValue = 7 ^ 5, or 16,807.
Anytime that the computer sees something trapped within parentheses, it calculates those values first. Then it uses its normal rules of precedence to figure out how to calculate the rest of the formula.
Use parentheses to enclose only one mathematical operator at a time, such as (3 + 4). Although you can use parentheses to enclose multiple mathematical operators, such as (3 + 4 ^ 5), doing so essentially defeats the purpose of using parentheses to make clear what the computer is to calculate first. You can, of course, use multiple parentheses to create fairly complex formulas, such as in the following formula:
EasyTaxCode = ((3 + 4) ^ 5 / 3 - 8) / 5 * -7
(Without the parentheses in the preceding formula, Liberty BASIC calculates an entirely different result.)
100 Part II: Learning Programming with Liberty BASIC
Using Liberty BASIC’s Built-In
Math Functions
By combining mathematical operators, you can create practically any type of mathematical formula. But creating some mathematical formulas may prove too cumbersome, so as a shortcut, Liberty BASIC (and many other programming languages) provides built-in mathematical functions that you can use, as shown in Table 8-2.
Table 8-2 |
Liberty BASIC’s Built-in Mathematical Functions |
Function |
What It Does |
ABS (x) |
Returns the absolute value of x |
|
|
ACS (x) |
Returns the arccosine of x |
|
|
ASN (x) |
Returns the arcsine of x |
|
|
ATN (x) |
Returns the arctangent of x |
|
|
COS (x) |
Returns the cosine of x |
|
|
EXP (x) |
Returns a number raised to a specified power (x) |
|
|
INT (x) |
Returns the largest integer less than or equal to a specific |
|
number or expression |
|
|
LOG (x) |
Returns the natural logarithm of x (Note: The value of x |
|
must be a positive, nonzero number.) |
|
|
SIN (x) |
Returns the sine of x |
|
|
SQR (x) |
Returns the square root of x |
|
|
TAN (x) |
Returns the tangent of x |
|
|
If you don’t understand terms like arcsine or logarithm, you probably don’t need to use them anyway. The important point to remember is that all programming languages, such as Liberty BASIC, include built-in mathematical functions that you can use if you need them.
The preceding equation calculates the square root of nine (9), which is three (3).
To see how the Liberty BASIC mathematical functions work, run the following program and type different numbers (negative, positive, decimal, and so on) between 0 and 1.0 to see how the program works: