- •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 9: Making Decisions with Control Statements 125
Make sure that you don’t use the same value on two separate CASE statements, as happens in the following example:
PROMPT Type a number. “; Answer
SELECT CASE Answer
CASE 1, 2
NOTICE “This always prints if you type a 2.”
CASE 2, 3
NOTICE “This never prints if you type a 2.”
END SELECT
In the preceding SELECT CASE statement, the program prints This always prints if you type a 2 if the user types 2, but the program never prints the instructions under the second CASE statement (CASE 2, 3). That’s because the first CASE statement runs first, preventing the second CASE statement from getting a chance to run at all.
Checking a relational operator
Sometimes, checking for an exact value or a range of values may still prove too limiting. You may, for example, compare a variable to another value by using one of those friendly symbols known as relational operators. A relational operator enables the SELECT CASE statement to determine whether a variable is greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), or not equal to (<>) a specific value.
To use a relational operator, you must use a slightly different version of the CASE statement as shown in the following example:
INPUT “How many cats do you own”; Answer
SELECT CASE
CASE (Answer <= 5)
PRINT “You need more cats.”
CASE (Answer > 5)
PRINT “Are you out of your mind?”
END SELECT
END
If the user types any number equal to or less than 5, the program prints, You need more cats. If the user types any number greater than 5, the program prints, Are you out of your mind?
126 Part II: Learning Programming with Liberty BASIC
Beware of the SELECT CASE statement in C/C++ and Java
In BASIC and many other programming languages such as Pascal, the SELECT CASE statement runs only one set of instructions the moment that it finds a match, such as printing
You’re still officially a teenager. in the Liberty BASIC example in the section, “Working with SELECT CASE Statements,” earlier in this chapter, if the user types the number 19.
C/C++ and Java programs, however, behave much differently. If you use these languages, you must specifically tell the computer to stop following instructions in a SELECT CASE statement (technically known as a switch statement in C/C++ and Java) by using the command break.
Consider, for example, the following C program:
#include <stdio.h> main ()
{
char akey;
printf (“Type a lower case letter “);
scanf(“ “);
scanf (“%c”, &akey); switch (akey) {
case ‘a’: printf (“You pressed the A key.\n”);
case ‘b’: printf (“You pressed the B key.\n”);
}
}
If you run this program and press the A key, this C program prints the following:
You pressed the A key.
You pressed the B key.
In C/C++ and Java, the computer follows every set of instructions in the switch statement from the first match that it finds to the end. To make sure that a C/C++ or Java program stops following any instructions in a switch statement, you must insert the break command as follows:
#include <stdio.h> main ()
{
char akey;
printf (“Type a lower case letter “);
scanf(“ “);
scanf (“%c”, &akey); switch (akey) {
case ‘a’: printf (“You pressed the A key.\n”);
break;
case ‘b’: printf (“You pressed the B key.\n”);
}
}
If you eventually plan to program in C/C++ or Java, you need to remember this subtle difference or you may find your C/C++ or Java programs acting different from similar BASIC programs.
The two crucial differences when using relational operators in a SELECT-CASE statement is:
Chapter 9: Making Decisions with Control Statements 127
The SELECT CASE variable (which is “Answer” in the above example), does not appear directly after the SELECT CASE command.
The relational expression (such as “Answer <= 5”) appears directly after each CASE statement.
Make sure that your relational operators don’t overlap another part of the SELECT CASE statement, as happens in the following example:
SELECT CASE
CASE (Answer < 10)
PRINT “This always prints.”
CASE (Answer < 12)
PRINT “This prints only if the user types 11.”
END SELECT
In this SELECT CASE statement, the program prints This always prints if the user types any number less than 10, but the program prints the instructions under the second CASE statement (CASE IS < 12) only if the user types 11.
128 Part II: Learning Programming with Liberty BASIC
Chapter 10
Repeating Yourself with Loops
In This Chapter
Looping with the WHILE-WEND commands
Looping a fixed number of times with the FOR-NEXT loop
In general, programmers try to get the computer to do as much as possible so that they can do as little as possible. Ideally, you want to write the
smallest programs possible, not only because small programs are easier to debug and modify, but also because smaller programs require less typing.
One way that programmers write as little as possible is by using programming structures known as loops. The idea behind a loop is to make the computer repeat one or more instructions. Consider, for example, the following program that prints the numbers 1 through 5 on-screen:
PRINT 1
PRINT 2
PRINT 3
PRINT 4
PRINT 5
END
If you want to expand this program to print out five million numbers, guess what? You must type five million instructions. Because you really don’t want to type that many instructions, you can use loops to make the computer repeat the same instructions multiple times. The computer does the hard work. Consider the following, which in programming lingo is called a FOR-NEXT loop:
FOR I = 1 TO 5
PRINT I
NEXT I
END
130 Part II: Learning Programming with Liberty BASIC
This program just prints out the following:
1
2
3
4
5
As you can see, when you use a loop, you can write shorter programs.
If you run this program, it does exactly the same thing as the first BASIC program. The loop version of this program, however, can print out five numbers or five million numbers (by changing the number 5 in the program to 5000000). Loops make the computer do more without forcing you to type additional instructions.
Although loops can help create shorter programs, the tradeoff is that loops are also harder to read and understand than a straightforward list of instructions. If you create a loop, make sure that you write a comment in the program to explain exactly what you expect the loop to do. (See Chapter 7 for more information about comments.)
A loop forces the computer to run the same instructions over and over, but eventually the computer needs to know when to stop. To tell the computer when to stop looping, you use a condition that represents either true or false.
In the world of mathematics and programming, anything that represents a true or false value is known as a Boolean expression; see Chapter 9. Some examples of Boolean expressions are 4 > 9.48 (which in Boolean arithmetic represents as a false value).
Using the WHILE-WEND Loop
Loops are handy for repeating one or more instructions. Of course, whenever you create a loop, you need to make the loop stop eventually.
One of the most common problems with loops is something known as an endless loop, which means that the computer follows a set of instructions but never stops. If a program gets caught in an endless loop, the program may appear to freeze on-screen. Usually, the only way to get out of an endless loop is to turn the computer off and then on again.
One way to make a loop eventually stop is to check if a certain condition is true. To do this in Liberty BASIC, you can create a special loop called a WHILE-WEND loop that looks as follows:
Chapter 10: Repeating Yourself with Loops 131
WHILE (Boolean expression is true) ‘ One or more instructions
WEND
To repeat one or more instructions, you just sandwich them between the WHILE and WEND commands, as shown in the following example:
I = 1
WHILE I < 5
PRINT “The square of “; I; “ is “; I * I
I = I + 1
WEND
END
This program does the following:
1.The first line creates the variable I and sets its value to 1.
2.The second line tells the computer that a loop is starting and that the computer is to run the instructions between the WHILE and WEND commands as long as the Boolean expression I < 5 is true.
3.The third line tells the computer to print The square of 1 is 1 the first time that the WHILE-WEND loops runs and The square of 2 is 4 the second time that the WHILE-WEND loop runs, and so on.
4.The fourth line tells the computer to add one to the value of the variable I, so now I represents the number 1 + 1, or 2.
5.The fifth line tells the computer to check whether the Boolean expression I < 5 is true. If it’s true, the program skips to the sixth line. If it’s not true (if I represents the number 1, 2, 3, or 4), the program returns to the top of the loop on the second line. The computer repeats the loop four times to print out the following:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
6. The sixth line tells the computer that the program is at an end.
Exiting a WHILE-WEND loop prematurely
Normally the only way to exit out of a WHILE-WEND loop is to wait until a certain condition becomes true. However, Liberty BASIC (like many other programming languages) also allows you to exit out of a loop prematurely using the magic EXIT WHILE command as follows: