- •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 8: Crunching Numbers and Playing with Strings 101
PROMPT “Type in a number”; AnyNumber
PRINT “The ABS value = “; ABS(AnyNumber)
PRINT “The ACS value = “; ACS(AnyNumber)
PRINT “The ASN value = “; ASN(AnyNumber)
PRINT “The ATN value = “; ATN(AnyNumber)
PRINT “The COS value = “; COS(AnyNumber)
PRINT “The EXP value = “; EXP(AnyNumber)
PRINT “The INT value = “; INT(AnyNumber)
PRINT “The LOG value = “; LOG(ABS(AnyNumber))
PRINT “The SIN value = “; SIN(AnyNumber)
PRINT “The SQR value = “; SQR(AnyNumber)
PRINT “The TAN value = “; TAN(AnyNumber)
END
You can use only positive numbers with the LOG function or in calculating a square root.
The arcsine and arccosine functions can accept only a number between 0 and 1.0. If you choose a higher or lower number, neither function works, and Liberty BASIC displays an error message.
Manipulating Strings
Besides manipulating numbers, computers can also manipulate strings. A string is anything that you can type from the keyboard, including letters, symbols (such as #, &, and +), and numbers.
In Liberty BASIC, a string is anything that appears inside quotation marks, as in the following example:
PRINT “Everything enclosed in quotation marks”
PRINT “is a string, including the numbers below:”
PRINT “72 = 9 * 8”
PRINT “You can even mix letters and numbers like this:”
PRINT “I made $4,500 last month and still feel broke.”
END
In the preceding program, the formula 72 = 9 * 8 is actually a string, even though it consists of numbers. That’s because Liberty BASIC treats anything inside quotation marks as a string, including any numbers inside of quotation marks.
102 Part II: Learning Programming with Liberty BASIC
How C/C++ handles strings
Unlike BASIC (and many other languages such as Pascal and Java), the C/C++ language doesn’t use a string data type. Instead, C/C++ programs use a more primitive data type known as a character (abbreviated char).
A character data type can hold only one character (such as a letter, symbol, or number), so to manipulate strings, C/C++ programs must use an array of characters. (Don’t worry. Read more about arrays in Chapter 16. The important thing right now is to realize that C/C++ programs must handle strings differently from BASIC.)
Just to give you an idea of how C/C++ programs handle strings, look at the following program. In
this example, this C program defines the variable myrelative and defines it as an array that can hold 10 characters:
main ()
{
char myrelative[10]; printf (“Type the name of a
male relative you hate.\n”);
scanf (“%s”, &myrelative); printf (“%s”, myrelative); printf (“ says he doesn’t like
you either!”);
}
Declaring variables as strings
As with numbers, you can use strings directly in your program, as follows:
PRINT “Print me.”
PRINT 54
END
Just as with numbers, you may want to store strings in variables so you can reuse that particular string over and over again by typing just the variable rather than the entire string. That way, your program can receive a string, store the string in a variable, and then manipulate the string to create a useful result, such as displaying a message to the user on-screen.
As you create a variable, you must tell Liberty BASIC, “Hey, I want this variable to hold only strings!” In technical terms, you’re declaring a string as a string data type. To create a variable to hold a string, Liberty BASIC enables you to create a variable and add the dollar sign ($) to the end of the variable name, as in the following example:
StringVariable$ = “This variable can hold only strings.”
Chapter 8: Crunching Numbers and Playing with Strings 103
If you fail to declare a variable as a string data type, but you still try to stuff a string into the variable, Liberty BASIC displays an error message and prevents your program from running. By ensuring that you store the correct data in variables, compilers such as Liberty BASIC try to make sure that you write programs that won’t have any unexpected bugs.
Smashing strings together
Unlike with numbers, you can’t subtract, divide, or multiply strings. But you can add strings (which is technically known as concatenating strings). To concatenate two strings, you use the plus sign (+) to essentially smash two strings into a single string, as the following example shows:
PROMPT “What is your name?”; MyName$
PRINT “Hello, “ + MyName$ + “. Isn’t it time to take”
PRINT “an early lunch from your job now?”
END
This Liberty BASIC program tells the computer to do the following:
1.The first line tells the computer to print the message What is your name? on-screen and then wait for the user to type something. Liberty BASIC stuffs whatever the user types into the string variable MyName$.
2.The second line tells the computer to create one big string that uses the string it’s storing in the MyName$ variable. If it’s storing the name “Tasha” in the MyName$ variable, the third line prints, Hello, Tasha. Isn’t it time to take.
3. The third line prints, an early lunch from your job now?.
4. The fourth line tells the computer that the program is at an end.
If you concatenate strings, make sure that you leave a space between the two strings so that they don’t appear smashed together (likethis). In the previous example, notice the space in the second line following the word Hello and the comma.
Playing with Liberty BASIC’s
String Functions
If just storing and concatenating strings were all that you could do, Liberty BASIC may seem pretty boring to you. That’s why Liberty BASIC includes a bunch of built-in functions to give you all sorts of fun ways to manipulate strings.
104 Part II: Learning Programming with Liberty BASIC
Playing with UPPERCASE and lowercase
If strings consist of letters, they can appear in the following three ways:
in all lowercase, like this
IN ALL UPPERCASE, LIKE THIS (WHICH CAN LOOK ANNOYING AFTER A WHILE)
As a mix of UPPERCASE and lowercase letters, Like This
To convert every character in a string to lowercase, you can use the special function LOWER$. To convert every character in a string to uppercase, you can use another function — UPPER$.
Both functions take a string and convert it to either uppercase or lowercase, as in the following example:
UPPER$(“hello”) ‘ HELLO
LOWER$(“GOOD-BYE”) ‘ good-bye
Run the following program to see how these two functions work:
PROMPT “What would you like to convert”; ThisString$
PRINT “This is what happens when you use LOWER$:”
PRINT LOWER$(ThisString$)
PRINT “This is what happens when you use UPPER$:”
PRINT UPPER$(ThisString$)
END
If you run this program and type in the string I Don’t Like Anyone Who
Copies My Work, the program displays the following:
This is what happens when you use LOWER$: i don’t like anyone who copies my work
This is what happens when you use UPPER$:
I DON’T LIKE ANYONE WHO COPIES MY WORK
Both the LOWER$ and UPPER$ functions work only with letters. They don’t do anything with symbols (such as $, %, or @) or numbers.
Counting the length of a string
If you need to manipulate strings, you may want to know the actual length of a string. To count the number of characters in a string, you can use the LEN function, which looks as follows: