• Aucun résultat trouvé

Comments: The Rest of the Story

Dans le document Turbo Pascal® Tutor (Page 108-114)

An important part of any serious program is its documentation-text that explains what the program does and why. Comments are used to help the reader of the program understand how it works. Comments explain the uses of identifiers, the actions of the program, the situation that will occur when a certain condition is met, the date the program was written, the name of the author, and anything else the author thinks is relevant.

Commenting takes a little effort, but it is an invaluable aid in debugging, maintaining, and enhancing your programs. Someone other than yourself may have to modify the source code of your program someday: If your program didn't have comments, it would be like cooking a gourmet meal from a recipe that lists all the ingredients but gives no instructions on how to combine them.

As mentioned in Chapter 6, comments are ignored by the compiler, which means they have no effect whatsoever on an object program: They do not increase its size or affect its execution speed.

Comments begin with a left brace (0 and end with a right brace 0). These symbols are known as comment delimiters. A comment can start and end almost anywhere, and it can occupy as many lines as needed.

About the only potentially confusing aspect of comments is when you use nested comments-comments embedded within comments. Say you've written the following section of a program:

WriteLn (date);

old a := a;

ReadLn (a);

{ Write the date } { Save the old value of a } { Read a new value for a }

Suppose you want to temporarily remove the last two statements shown.

You don't want to delete them from the program forever, just keep them from being performed during this run. This can be done by "commenting out" a section of code. At first glance, it appears that you could simply put

comment symbols before and after the statements you want to comment ({Write the date}) and understands that the next left brace (at the beginning of the second line) is the beginning of another comment. However, when the compiler encounters the right brace at the end of the third line, it assumes the comment is over. Thus, the statement ReadLn (a) is compiled into the program, even though we didn't want that to happen.

In this particular case, the compiler then finds the extra right brace in the last line of the example and signals with an error message that something is wrong. Sometimes, however, this sort of error goes undetected, causing many debugging headaches.

One solution to this problem is to delete some of the comment symbols within the section that is commented out. But that would be more trouble (and probably cause more errors) than removing the entire section of code.

Fortunately, there's a better way.

There's an alternate pair of comment symbols: a left parenthesis paired with an asterisk 1/(*" to begin a comment and an asterisk paired with a right parenthesis 1/*)" to end a comment.

Turbo Pascal allows you to place one kind of comment within the other, as a natural result of the way comments work. If you begin a comment with a left brace (0, everything after it is ignored, including the set of parentheses and asterisks

«*

and

*»,

until the right brace (}) appears. The reverse is also true: When a comment begins with a left parenthesis and an asterisk

«*),

everything up to the next asterisk and right parenthesis (*) is ignored, including a set of braces ({

n.

So, the solution to the problem of nested comments is to always use one set of comment delimiters for descriptive comments, and the other set of comment delimiters for commenting out sections of code. In this manual, we use curly braces as comment delimiters for ordinary text comments. If the situation requires that sections of code be commented out, we use the

Program Structure 87

parenthesis-asterisk comment delimiters. Now, let's rewrite our latest example using both sets of comment delimiters:

WriteLn (date);

(*

old a := a;

ReadLn (a);

*)

{ Write the date Save the old value of a { Read a new value for a

The program runs just fine now, with the sections of code between (* and *) ignored by the compiler. You can use this technique to test various parts of programs, to isolate problems, or to prove that a section of your program does what you intend.

You can insert comments almost anywhere, except in the middle of an identifier or a reserved word, or inside a string. In the first case, the compiler thinks that the reserved word or identifier ends where the comment begins; in the second, it thinks that the comment is part of the string. The statement

WriteLn('Hello, world, my name is {not} Joe.');

produces the output

Hello, world, my name is {not} Joe.

Review

The structure of a Pascal program follows this form:

program Name ({optional file identifiers});

label label declarations here

const {constant declarations here

type type declarations here

var variable declarations

{ subprograms (procedures and functions) declared here } begin

{ main body of program } end.

Pascal is a free-format language. It allows declarations and statements to be formatted in many ways, subject to certain simple constraints. Pascal provides a rich variety of statements from which to choose.

Turbo Pascal is more flexible than Standard Pascal in that label, const, type, var, and subprogram declarations can be placed in any order and can occur more than once.

Comments are used to document programs and to prevent certain sections of code from being run during tests. The two sets of comment delimiters-{

and } and (* and *)-allow the programmer to nest comments within one another. Comments can be used anywhere, except in the middle of a string, a reserved word, or an identifier.

Now you're ready to tackle predefined data types.

Program Structure 89

c

H A p T E R

8

Predefined Data Types

In this chapter, we'll explore Turbo Pascal's predefined data types-the ones that you will use most often. They include Integer, LongInt, Real, Boolean, and Char. We'll explain and give examples of each. In addition, we'll cover string types, which allow you to manipulate textual data.

Every data object in a Pascal program has a type. You must therefore tell the compiler the type of every constant and every variable your program uses.

For constants, the type is implicit in its declaration. For example, const

peopleCount = 13;

e = 2.71818;

Given these declarations, the compiler figures out that peopleCount is an Integer, because it fits in the range of integers and has no decimal point.

Similarly, e's decimal point marks it as a Real.

In determining the type of variables, the compiler doesn't have to guess, because you are required to explicitly state the type in the declaration:

var

HeadCount: Integer;

Acreage: Real;

Pascal's data types can be classified as either simple types, which are used for information that is always manipulated as a whole, and structured types, which are used for information consisting of smaller pieces that can be manipulated individually or as a whole.

Predefined Data Types 91

Turbo Pascal's predefined data types are the ones that you will use most often. All of Turbo's predefined types fall into the simple category; they include Integer, LongInt, Real, Boolean, and Char. String types allow you to manipulate textual data; while not part of Standard Pascal, strings are a common extension.

Integers

Integers are whole numbers, negative and positive, in the range -32768 to 32767. 12 is an integer, as are 456 and -12000. On the other hand, 1.234,2.0, and -0.54234312 are not: They have decimal points. You use Integer data types when your data is strictly numeric, doesn't contain fractions, and fits in the prescribed range. Figure 8.1 shows the syntax diagram for an unsigned integer (an integer constant without a + or - sign).

unsigned integer ----.--;~ digit sequence

®--+

hex digit sequence

Figure 8.1 : Syntax Diagram of an Unsigned Integer

In Turbo Pascal, you can specify an integer constant in hexadecimal (base 16, also known as hex) notation as well as ordinary decimal (base 10) notation.

A hexadecimal constant consists of a series of hex digits (0 through 9 or a through

t>

preceded by a dollar sign ($). Declaring hex constants is an advanced technique we'll delve into in Chapter 25.

A Turbo Pascal integer occupies 2 bytes of memory. Because there are only so many unique values that can be expressed by 16 bits (65,536, to be exact), objects of type Integer are limited to the range -32768 through 32767. Each integer you declare will occupy 2 bytes regardless of its value; for example, the integers 0, 37, and 32,000 are all stored in 2 bytes. For your convenience, there's a predefined constant called MaxInt equal to 32767.

Dans le document Turbo Pascal® Tutor (Page 108-114)

Documents relatifs