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

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.