
- •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 5: Getting Your Hands on a Real Language: Liberty BASIC |
69 |
||
|
|
||||
|
|
|
|
|
|
|
Keystroke Command |
What It Does |
|
|
|
|
Backspace |
Deletes the character to the left of the cursor |
|
||
|
|
|
|
|
|
|
Ctrl+F |
Finds and replaces text in your program |
|
||
|
|
|
|
|
|
|
Ctrl+A |
Selects your entire program |
|
||
|
|
|
|
|
|
|
Ctrl+Z |
Takes back the effects of the last command you |
|
||
|
|
|
chose (the Undo command) |
|
|
|
|
|
|
|
|
Getting Help Using Liberty BASIC
As do most programs, Liberty BASIC provides an online Help system. To access Help, follow these steps:
1.Choose Help Liberty BASIC Help.
The Help Contents window appears, listing various Help topics, as shown in Figure 5-3.
Figure 5-3:
The Liberty BASIC Help window provides lists various topics that can answer any questions you might have using Liberty BASIC.

70 |
Part II: Learning Programming with Liberty BASIC |
2.Click a Help topic that you want to read.
Depending on the topic you choose, the Help window displays different information about using Liberty BASIC.
3.Click the Close box of the Help window after you finish.
Exiting Liberty BASIC
Eventually, you may need to exit Liberty BASIC so that you can do something else with your computer. To exit Liberty BASIC, follow these steps:
1.Choose File Exit from the menu bar, or click the close box (which appears in the upper right-hand corner) of the Liberty Basic window.
If you haven’t saved the Liberty BASIC program you’re currently displaying, a dialog box asks whether or not you want to save the file.
2.Click Yes to save the current file or click No if you don’t want to save your changes.
Another dialog box appears, asking whether you’re sure you want to exit Liberty BASIC.
3.Click Yes to exit (or No to return to Liberty BASIC).
Liberty BASIC gracefully exits.

Chapter 6
Handling Input and Output
In This Chapter
Getting input and output the primitive way
Getting input and output the modern way
Printing stuff out
Every program takes in data (input), manipulates that data in some way, and spits the data back out in some form (output).
In the old days, programmers gave input to the computer by using a variety of methods, ranging from physically rearranging switches on the computer to using paper tape, punch cards, teletype machines (which resembled typewriters), and finally keyboards and touch-screen monitors. As computers spit back some form of output, it usually appears on paper or on-screen on a monitor.
Despite the fact that today’s computers include pop-up windows, dialog boxes, command buttons, and scroll bars, most of today’s programming languages are still rooted in the past, when programs waited for the user to type a name or a number. Then the program shoved that information up a line and printed the resulting output directly below it.
That’s why most programming languages such as BASIC (and even C/C++) contain built-in commands for reading and displaying data one line at a time on-screen. Naturally, such programs look fairly primitive, especially if you compare them with today’s modern programs, but be patient. As you’re learning programming for the first time, understanding how programs can accept input and spit data back out is much more important (at this time) than worrying about how a program actually looks on-screen.
Inputting and Outputting Data:
The Old-Fashioned Way
Input occurs whenever a program accepts data from an outside source. Some examples of where a program can get input include the following:

72 |
Part II: Learning Programming with Liberty BASIC |
Anything the user types from the keyboard
The movement of the computer mouse
Data that someone previously stores in a file (such as a word processor document or the high score of a video game)
Data that feeds into the computer from an outside source (such as a Web page sent through a modem or an image captured through a scanner)
Output occurs whenever a program displays data back, usually after it manipulates the data in some way. Some common examples of output include the following:
Data that appears on-screen (such as text, pictures, or video images)
Data that prints on paper through a printer
Sound that plays through a computer’s speakers (such as audio files previously downloaded off the Internet)
In BASIC, the simplest way to output data is to use the PRINT command, which simply displays whatever appears inside double quotes. In the following example, the BASIC command does nothing but display the text What are you looking at? on-screen, as shown in Figure 6-1.
PRINT “What are you looking at?”
END
The PRINT command in Liberty BASIC displays text in a window known as the main window. If you don’t want the main window to appear, just use the command NOMAINWIN. Later when you start writing programs that use a graphical user interface (as explained in Chapter 14), you won’t need to display the main window to display data on-screen.
As the PRINT command gives your program the capability to output data, the INPUT command gives your program the capability to accept data from the keyboard. To use the INPUT command, you simply type the word INPUT, following it with a character or phrase that you want to represent whatever the user types at the keyboard, as in the following example:
PRINT “What is the name of your boss?”
INPUT Myboss$
PRINT Myboss$ + “? That sounds like the name of a moron!”
END
If you type the preceding example into Liberty BASIC, the program displays the text What is the name of your boss? If you type a name and press Enter, the program responds by displaying the name that you typed, following it with a question mark and the text That sounds like the name of a moron!, as shown in Figure 6-2.

Chapter 6: Handling Input and Output |
73 |
An equivalent C program
Unlike BASIC, other programming languages force you to enclose instructions inside special words or symbols that act as parentheses (so to speak) that wrap around the beginning and ending of your program. In the C/C++ language, for example, the smallest program you can write consists of the word main, which you follow with parentheses and curly brackets, as in the following example:
main ()
{
}
If you write instructions by using C/C++, you must enclose them inside these strange curly brackets. Just to show you how drastically different identical programs can look if you write
them in different programming languages, here’s an equivalent C program that enables the user to type a name (as input) and displays onscreen whatever name the user types, following it with the words, That sounds like the name of a moron!:
main ()
{
char myboss[15];
printf (“What is the name of your boss.\n”);
scanf (“%s”, &myboss); printf (“%s”, myboss); printf (“? That sounds like
the name of a moron!”);
}
Figure 6-1:
The main window is a special window for displaying text from the
command.