- •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 7: Variables, Constants, and Comments |
87 |
If you want the user to type a string (such as a name) into the Prompt dialog box, you need to add a dollar sign at the end of the variable to hold the string, such as YourName$. The dollar sign just tells Liberty BASIC that this particular variable holds only a string, which can consist of a name, a ZIP Code, or a street address.
In the following example, the Prompt dialog box stores a string:
NOMAINWIN
PROMPT “What is your name?”; YourName$
Message$ = YourName$ + “, you deserve a raise!”
NOTICE Message$
END
This Liberty BASIC program tells the computer to do the following:
1.The first line tells Liberty BASIC not to display the main window.
2.The second line displays a Prompt dialog box that asks, What is your name? Liberty BASIC stores whatever you type in the variable
YourName$.
3.The third line adds the string , you deserve a raise! to the string that Libery BASIC stores in the YourName$ variable. This combination of
“, you deserve a raise!” and the YourName variable Liberty BASIC stores in the variable Message$.
4.The fourth line creates a Notice dialog box that displays the string that Liberty BASIC stores in the Message$ variable.
5.The fifth line tells Liberty BASIC that the program is at an end.
If the user types a number in the Prompt dialog box in Step 2, such as 45, Liberty BASIC treats that number as just a string of symbols, such as 45.
Declaring your variables
Variables enable a program to store and manipulate data. As a result, identifying all the variables that a program uses and what type of data it stores in them can help you understand how a specific program works.
Unlike most programming languages, the BASIC programming language enables you to create and use variables anywhere in a program. Although this capability can prove convenient while you’re writing a program, you may find it difficult to understand later while you’re trying to modify that same program.
Study, for example, the earlier Lizzie Borden program. Quick: How many variables does this program use? If you can’t tell right away, you must waste time
88 |
Part II: Learning Programming with Liberty BASIC |
going through the entire program, line by line, to find the answer. (The answer is seven variables: Parents, Whacks, MotherAxWhacks, FatherAxWhacks, FirstName$, LastName$, and FullName$.)
To enable you (or anyone else) to more easily identify all the variables that a program uses, most programming languages, such as C/C++ and Pascal, force you to declare your variables at the beginning of your program. Declaring your variables at the beginning has the following two purposes:
To identify the names of all variables that a program uses
To identify the total number of variables that a program uses
Knowing the total number of variables that a program uses can help you better understand how a program works because you can determine all the places where the program may store data.
Liberty BASIC supports the original (and some may claim a purer) dialect of the BASIC programming language, which lacks modern programming language constructs that have been added in later versions of the BASIC programming dialect, such as variable declarations and constants.
To declare a variable ahead of time in some versions of BASIC, such as Visual Basic (but not Liberty BASIC), you use the DIM command, as follows:
DIM Eyeballs
The preceding command tells your computer to create a variable by the name of Eyeballs. You can define multiple variables at once, just by separating them with a comma, as the following example shows:
DIM Eyeballs, Bullets, Logs
The preceding command tells your computer to create three variables by the names of Eyeballs, Bullets, and Logs.
Now if you rewrite the preceding Lizzie Borden program and declare all variables at the start of the program, you can easily identify and count all variables that the program uses. As you can see in the following revised version, written in QBASIC (another BASIC dialect similar to Liberty BASIC), this program declares variables ahead of time so that you can easily count and identify all the variables that the program uses:
Chapter 7: Variables, Constants, and Comments |
89 |
DIM Parents, Whacks, MotherAxWhacks, FatherAxWhacks
DIM FirstName$, LastName$, FullName$
Parents = 2
Whacks = 20
MotherAxWhacks = Parents * Whacks
FatherAxWhacks = MotherAxWhacks + 1
FirstName$ = “Lizzie”
LastName$ = “ Borden”
FullName$ = FirstName$ + LastName$
PRINT FullName$ + “ had an ax, gave her mother “; MotherAxWhacks;
PRINT “ whacks. When she saw what she had done, gave her”; PRINT “ father “; FatherAxWhacks
END
In this example, you can quickly see that this program uses three variables to hold strings (FirstName$, LastName$, and FullName$) in addition to four variables to hold values (Parents, Whacks, MotherAxWhacks, FatherAxWhacks).
An equivalent Java program
Java closely resembles C/C++, so if you know C/C++, you should little trouble learning Java. Just to give you some exposure to what a Java program looks like, study the following program to get a better idea how another programming language accomplishes the same task as the QBASIC program in the accompanying text:
public class TrivialApplication
{
public static void main(String args[]) {
int parents, whacks, motheraxwhacks, fatheraxwhacks; String firstname, lastname,
fullname; parents = 2; whacks = 20;
motheraxwhacks = parents * whacks;
fatheraxwhacks = motheraxwhacks + 1;
firstname = “Lizzie”; lastname = “ Borden”; fullname = firstname + last-
name; System.out.println(fullname +
“ had an ax, gave her mother “ + motheraxwhacks);
System.out.println(“whacks. When she saw what she had done, gave her”);
System.out.println(“ father “ + fatheraxwhacks);
}
}
If you run this Java program, it behaves just as the QBASIC version does.