Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

90

Part II: Learning Programming with Liberty BASIC

Defining the type of data that variables can hold

In addition to enabling you to create and name your variables at the beginning of a program, most programming languages (but not Liberty BASIC) also force you to define the type of data that each variable can hold. Defining your data types serves the following two purposes:

It identifies the type of data that each variable can hold. If you clearly identify what type of data a variable can hold, you (or another programmer) can better understand where a program may store data and what type of data it can store in each specific variable.

It prevents bugs by keeping variables from storing the wrong types of data by mistake.

Just so that you can see how other programming languages declare variables and the data types that they can hold, the following example shows

how the C programming language declares a variable by the name of IQ to hold an integer value:

main ()

{

int IQ

}

In the Pascal programming language, that same variable declaration may look as follows:

Program Main;

Var

IQ : integer;

End.

And in certain BASIC dialects, such as Visual Basic, that same variable declaration looks like the following example:

DIM IQ AS INTEGER

Declaring variables isn’t just for the convenience of the computer; it’s for the convenience of the programmer who must read, understand, and modify a program later.

Using Constants

The value stored in a variable can be changed while the program’s running; that’s why they’re called variables (because their values can vary). Sometimes, however, you may want to use a fixed value throughout your program. Look, for example, at the following program. Can you figure out what the number .1975 stands for?

Balance = 43090

OurProfit = Balance * .1975

Balance = Balance + OurProfit

PRINT “Pay this amount, or we’ll get you = “; Balance

PRINT “Today’s current loan sharking interest rate = “; .1975 END

Chapter 7: Variables, Constants, and Comments

91

A quick glance at the preceding program shows that the meaning of the number .1975 isn’t obvious. Because the meaning of numbers isn’t always clear without additional explanation, programmers use constants. By using a constant, you can use a descriptive name to represent a fixed value.

Liberty BASIC supports an older version of the BASIC programming language that doesn’t support constants. However, other versions of BASIC (such as Visual Basic and most other programming languages) do support constants.

To see how constants work, study the following program written in the Pascal programming language:

Program UnderstandingConstants; Const

InterestRate = 0.1975; Var

OurProfit : real; Balance : real;

Begin

Balance := 43090;

OurProfit := Balance * InterestRate; Balance := Balance + OurProfit;

Writeln (‘Pay this amount or we’ll get you! = ‘, Balance:6:2);

Writeln (‘Today’s current loan sharking interest rate = ‘, InterestRate:1:4);

End.

If you run this program, you’ll see the following output:

Pay this amount or we’ll get you = 51600.28

Today’s current loan sharking rate = 0.1975

As you can see in the above program, the value of 0.1975 is used twice in the program (on lines 9 and 12). If the interest rate changed from 0.1975 to 0.2486 and your program did not use constants, you would have to exhaustively search through your entire program and change every line that uses the value of 0.1975 to 0.2486. For short programs like the above program, this is tolerable. In huge programs, however, this would be time-consuming and error-prone.

So to save time and ensure accuracy, programmers use constants. By using constants in the above Pascal programming example, you only need to change the value of the constant InterestRate (in line 3) from 0.1975 to 0.2486. The computer automatically inserts this new value in lines 9 and 12, which also contain the constant InterestRate.

92

Part II: Learning Programming with Liberty BASIC

Constants have the following two key advantages:

They identify numeric or string values with a descriptive name.

Changing the value of a constant automatically modifies your entire program quickly and accurately.

Commenting Your Code

If you write a small program, anyone can readily understand how it works by following it line-by-line. But if you write a large program, understanding what the program does can prove difficult for others (and even for you) without spending a long time studying each line.

To make understanding (and ultimately maintaining) a program easier (because programmers are a notoriously lazy bunch), every programming language enables you to insert comments into your source code. Comments enable you to store directly in your source code explanations to identify the following information:

Who wrote the program

The creation and last modification dates of the program

What the program does

How the program works

Where the program gets, saves, and outputs data

Any known problems with the program

In Liberty BASIC, you can add comments in one of the following two ways:

By using the REM (short for REMark) statement.

By using the apostrophe ().

The following program shows how to use both the REM statement and the apostrophe to insert comments into a program. Although you can use both types of comments in a single program, you want to use only one or the other for consistency’s sake.

Created on March 29, 2005

Written by John Doe

This program displays a not-so-subtle

message to potential copycats to

Chapter 7: Variables, Constants, and Comments

93

come up with their own ideas rather

than steal mine.

REM This program does nothing more than

REM print a message on-screen to

REM insult any potential authors browsing

REM through this book in hopes of stealing

REM ideas to use in a competing book.

NOMAINWIN ‘ Keeps the main window from appearing

NOTICE “Don’t steal ideas from this book!”

END ‘ This last line ends the program

Because comments are for the benefit of humans only, the computer looks at the preceding Liberty BASIC program as follows:

NOMAINWIN

NOTICE “Don’t steal ideas from this book!”

END

The apostrophe is more versatile than the REM statement for making a comment because the apostrophe can create a comment that appears as a separate line or as part of an existing line. The REM statement can create only a comment that appears on a separate line.

Comments exist solely for your benefit. The computer completely ignores any comments that you insert in a program. So make your comments useful but not too wordy; otherwise, they become more of a nuisance than an aid.

Comments can prove valuable for telling Liberty BASIC to temporarily ignore one or more lines of code. Rather than delete an entire line, test to see whether the program works, and then retype the previously deleted line; for example, you can just comment out the line, as follows:

NOMAINWIN

‘ A = SQR((B * B) + (C + C)) END

If you run this program, Liberty BASIC sees only the following:

NOMAINWIN

END

To restore the commented line, just remove the apostrophe so that Liberty BASIC sees the program as follows:

NOMAINWIN

A = SQR((B * B) + (C + C)) END

94

Part II: Learning Programming with Liberty BASIC

Chapter 8

Crunching Numbers and

Playing with Strings

In This Chapter

Performing mathematical operations

Using Liberty BASIC’s built-in math functions

Pulling strings with your data

Converting strings into numbers

One of the most important parts of a computer program is its capability to manipulate any data that it receives and to spit out a useful answer

that people are willing to pay for (so that you can make money). The two types of data that your program must manipulate are numbers and words (known as strings by the programming community).

Some common number-manipulating programs include spreadsheets, accounting programs, and even video games (because they need to calculate the correct way to display jet fighters or dragons popping on-screen to enable you to mow them down with a machine gun). Common string-manipulating programs include databases (which store, sort, and rearrange such data as names), word processors, and foreign-language translation programs.

Adding, Subtracting, Dividing,

and Multiplying

The four basic ways to manipulate numbers are adding, subtracting, dividing, and multiplying. By using these four mathematical operations, you can create any type of complicated mathematical formula.

To add, subtract, divide, or multiply two numbers (or two variables that represent numbers), you use the symbols shown in Table 8-1.