- •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
64 |
Part II: Learning Programming with Liberty BASIC |
Loading Liberty BASIC
As you install Liberty BASIC, the program creates its own directory and adds Liberty BASIC to your Program menu. To load Liberty BASIC, follow these steps:
1.Click the Start button on the Windows taskbar.
The Start menu opens.
2.Choose Programs Liberty BASIC Liberty BASIC.
The Liberty BASIC program appears.
To make loading Liberty BASIC easier and faster, you can place a Liberty BASIC icon on the Windows desktop.
To place a Liberty BASIC icon on your Windows desktop, follow these steps:
1.Right-click the Windows desktop.
A pop-up menu appears.
2.Choose New Shortcut.
A Create Shortcut dialog box appears.
3.Click Browse.
A Browse dialog box appears.
4.Find and click the Liberty BASIC program and then click Open.
You may need to change directories or drives to find the Liberty BASIC program if it isn’t stored in the C:\Program Files\Liberty Basic directory. After you click Open, the Create Shortcut dialog box appears again.
5.Click Next.
The Select a Title for the Program dialog box appears.
6.Type a name, such as Liberty BASIC, in the Select a Name for the Shortcut text box and click Finish.
The Liberty BASIC icon appears on your desktop. The next time that you want to load Liberty BASIC, just double-click this icon.
Your First Liberty BASIC Program
The Liberty BASIC editor is where you write, edit, and run your BASIC program. To see the power of Liberty BASIC, type the following into the Liberty BASIC editor:
Chapter 5: Getting Your Hands on a Real Language: Liberty BASIC |
65 |
PRINT “This BASIC program mimics a really bad boss.”
PRINT “What is your name?”
INPUT Name$
PRINT “Hello “ + Name$ + “. You’re fired! Have a nice day.” END
Liberty BASIC, like most versions of BASIC, doesn’t care whether you type commands in uppercase, lowercase, or a mixture of both. Most programmers, however, like to use all uppercase to identify all the BASIC commands that they use in a program.
Unlike a word processor, the Liberty BASIC editor doesn’t wrap words from one line to the other, which means that you can keep typing all the way to the far right until your text scrolls out of view.
This program tells the computer to perform the following tasks:
1.The first line prints the message This BASIC program mimics a really bad boss. on-screen.
2.The second line prints (adds) a blank line directly underneath the message.
3.The third line prints What is your name? on-screen.
4.The fourth line displays a question mark (?) as a prompt and waits for the user to type a name. As soon as the user presses the Enter key, the BASIC program stores whatever the user types into a temporary memory location that it identifies as Name$.
5.The fifth line prints the message “Hello (following it by the name that the user types in the fourth line). You’re fired! Have a nice day.” The plus sign (+) tells Liberty BASIC to add the word “Hello” with the words that it stores in Name$.
6.The sixth line tells the computer that this is the end of the program.
Running a Liberty BASIC program
After you finish typing a BASIC program, press Shift+F5 or choose Run Run from the Liberty BASIC menu bar to run the program. Figure 5-1 shows what the BASIC program from the preceding section looks like when run on Liberty BASIC.
As you run this program, Liberty BASIC displays the text in a window that it calls the main window. You can use the menu bar in the main window to print or save any text that appears in the main window.
66 |
Part II: Learning Programming with Liberty BASIC |
Main window
Figure 5-1:
Running your first Liberty BASIC program.
Saving a Liberty BASIC program
Although you can type your Liberty BASIC programs over and over whenever you want to run them, saving your program to a hard drive or floppy disk is much easier. Then you can load and edit the program later.
To save a program, follow these steps:
1.Choose File Save from the Liberty BASIC menu bar, or click the Save
File icon on the Liberty Basic toolbar as shown in Figure 5-2.
The Save As dialog box appears.
2.Type a name for your file in the Filename text box.
You may want to change folders or drives to store your Liberty BASIC file.
3.Click OK.
Liberty BASIC automatically adds a BAS file extension to Liberty BASIC programs that you save. Unless you have a good reason to change this file extension, use the BAS extension to help you identify your Liberty BASIC programs from any other files stored on your computer.
Chapter 5: Getting Your Hands on a Real Language: Liberty BASIC |
67 |
New file
Open file
Save file
Figure 5-2:
The Liberty BASIC toolbar provides shortcuts for accessing the most common Liberty Basic commands.
Loading or starting a Liberty
BASIC program
Liberty BASIC can display only one BASIC program on-screen at a time. Any time that you want to view another BASIC program, you must get rid of the BASIC program that you currently have on-screen.
To create a brand new program, follow these steps:
1.Choose File New File from the Liberty BASIC menu bar, or click the
New File icon on the Liberty BASIC toolbar.
If you haven’t saved the BASIC program you’re currently displaying, a Please Confirm dialog box appears.
68 |
Part II: Learning Programming with Liberty BASIC |
2.Click Yes to save the current file or click No if you don’t want to save your changes.
Liberty BASIC displays a blank screen, ready for you to type a new BASIC program.
To load a previously saved program, follow these steps:
1.Choose File Open from the Liberty BASIC menu bar, or click the
Open File icon on the Liberty BASIC toolbar.
If you haven’t saved the BASIC program you’re currently displaying, a
Please Confirm dialog box appears.
Liberty BASIC automatically remembers the last seven files you opened, so for a quick way to load a recently opened file, just click the File menu followed by the name of the file you want to open, which appears at the bottom of the File menu.
2.Click Yes to save the current file or click No if you don’t want to save your changes.
The Open a BAS File dialog box appears. You may need to change directories or drives to find where you saved the file.
3.Click the file that you want to open and then click OK.
Your chosen program appears.
Using Keystroke Commands
in Liberty BASIC
The Liberty BASIC editor works like a simple word processor. You can use the mouse or the keyboard to navigate a BASIC program. Table 5-1 shows the different keys that you can press and what they do.
Table 5-1 |
Commands for the Liberty BASIC Editor |
Keystroke Command |
What It Does |
Home |
Moves the cursor to the front of the line |
|
|
End |
Moves the cursor to the end of the line |
|
|
Shift+Arrow key |
Highlights text in the direction of the arrow |
|
|
Delete |
Deletes the character directly above the cursor or |
|
deletes an entire highlighted block of text |
|
|