- •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
76 |
Part II: Learning Programming with Liberty BASIC |
NOMAINWIN
PROMPT “What is the name of your boss?”; name$
NOTICE name$ + “? That sounds like the name of a moron!”
END
Sending Data to the Printer
One of the most popular ways to output data is to print it out on paper, also known as a hard copy. In Liberty BASIC, the commands for sending data to a printer are LPRINT and DUMP, as shown in the following example:
LPRINT “Save the dolphins! Kill the tuna!”
DUMP
END
The LPRINT command sends data to your default printer. The DUMP command simply tells your printer to start printing right away.
You can skip the DUMP command if you want, but then the LPRINT command may not start printing immediately.
The LPRINT command starts printing in the upper-left corner of the page. If you want to start printing your data in a different location on a page, you may need to add extra lines or spaces to change the vertical and horizontal position where your text starts printing.
To change the vertical position, just use the LPRINT command by itself, as follows:
LPRINT
LPRINT “Save the dolphins! Kill the tuna!”
DUMP
END
In the preceding example, the first LPRINT command prints a blank line, and the second LPRINT command prints the text Save the dolphins! Kill the tuna!
To change the horizontal position, use the SPACE$(x) command where the letter x represents how many spaces you want to insert. If, for example, you want to insert five spaces before printing any text, you use the SPACE$(5) command, as in the following example:
Chapter 6: Handling Input and Output |
77 |
LPRINT “Save the dolphins! Kill the tuna!”
LPRINT SPACE$(5); “Save the dolphins! Kill the tuna!”
DUMP
END
The preceding program would make your printer print the following message:
Save the dolphins! Kill the tuna!
Save the dolphins! Kill the tuna!
When using the SPACE$(x) command, you need to use the semicolon (;), following it with the data that you want to print, which you surround with double quotation marks.
This chapter provides a brief explanation for getting input and displaying output, just so you can understand the way a computer program gets data and spits it back out again for the user to see. For more details about creating a more modern user interface that offers windows, dialog boxes, and menus, skip to Chapter 14.
78 |
Part II: Learning Programming with Liberty BASIC |
Chapter 7
Variables, Constants,
and Comments
In This Chapter
Using variables
Creating and using constants
Adding comments to your code
When a program accepts input, the first thing the computer needs to do is find a place to store any data that it gets from the user. Because
computers are like one giant brain, they simply store data in memory.
Of course, if you stuff enough data into a computer, it’s likely to lose track of all the data in its memory (much like a person might do). So to help the computer find data that it already stored, computer programs use something called variables.
A variable simply acts like a storage bin. You can stuff any type of data into a variable, such as numbers or words, and then retrieve them back out again so you can stuff different data into that variable all over again. Although variables can only store one chunk of data at a time, they can be reused over and over again to store different data. The contents of a variable may vary at any given time, hence the name variable.
Besides storing data in variables, programs also use things known as constants and comments. Constants represent a fixed value that a program may need, and comments are explanations that programmers use to explain how a program works.
Although the idea of using variables, constants, and comments may seem mysterious to you, relax. You’ll see their purpose as soon as you start writing your own programs.