Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

Data values (in many languages including Pascal) are called constants, as they never

change during the program execution. In Pascal there are two types of constants:

Literal constants

Named constants

Literal constants are data values such as explicit numbers and text strings, while a

named constant is a “constant variable.” The difference between a named constant and

a variable is that the value of the named constant does not change during the program.

Like variables, a named constant is given a name and has to be declared in the declaration

part. Actually, the declaration part is divided into two sections, CONST and VAR;

the CONST section comes before the VAR section. Suppose that you would like to use

the value 3.14159 (a numerical constant known as Pi) many times in your calculations.

It would be more convenient to give it a name and use the name in your code. You can

declare named constants as in the following example:

CONST

Pi = 3.14159;

ThisYear = 1992;

Department= 'OtoRhinoLaryngology';

Some constants are predefined in Pascal as standard identifiers. One useful predefined

named constant is MAXINT, which gives the maximum value an integer can possess.

The value depends on the computer used. If you want to know the value of MAXINT

in your computer, use the statement:

WRITELN(MAXINT);

A typical value is 32,767 (two bytes).

In the following program, the perimeter of a circle is calculated using the named constant

Pi.

{ — — — — — — — — — — — — — — — - Example 1-9 — — — — — — — — — — — — — — - }

PROGRAM Constants(OUTPUT);

{ Constant Declarations }

CONST

Pi = 3.14159;

{ Variable Declarations }

VAR

Radius, Perimeter :REAL;

{ Program Block }

BEGIN

Radius := 4.9;

Perimeter := 2 * Pi * Radius;

WRITELN('Perimeter=', Perimeter)

END.

The output of this program is:

Perimeter= 3.0787582000E+01

NOTE If you are using Delphi or Turbo Pascal, you do not need to redefine the

constant Pi, as it is predefined as a standard identifier.

Type Conversion: ROUND, TRUNC

You can assign an integer to a variable of the type REAL, but the opposite is not permitted.

The reason for this is because the storage size allocated for an integer is

smaller than that allocated for a real number. If this were permitted, data could be lost

or corrupted when a large number was moved to a smaller location in which it did not

fit. You can, however, perform the conversion with one of the following two functions:

ROUND(n) rounds n to the closest integer

TRUNC(n) truncates the fraction part of n

where n is a real variable or expression.

Consider these examples:

ROUND(8.4) returns 8

ROUND(8.5) returns 9

TRUNC(8.4) returns 8

TRUNC(8.5) returns 8

As you can see in the examples, the two functions may or may not return the same

integer value for the same argument.

In the following program the two functions are used to get the rounded and the truncated

integer values of the real variable Perimeter

{ — — — — — — — — — — — — — — — Example 1-10 — — — — — — — — — — — — — — - }

PROGRAM Functions1(OUTPUT);

{ Constant Declarations }

CONST

Pi = 3.14159;

{ Variable Declarations }

VAR

Perimeter, Radius :REAL;

RoundedPerimeter, TruncatedPerimeter :INTEGER;

{ Program Block }

BEGIN

Radius := 4.9;

Perimeter := 2*Pi*Radius;

RoundedPerimeter := ROUND(Perimeter);

TruncatedPerimeter := TRUNC(Perimeter);

WRITELN('Perimeter=', Perimeter);

change during the program execution. In Pascal there are two types of constants:

Literal constants

Named constants

Literal constants are data values such as explicit numbers and text strings, while a

named constant is a “constant variable.” The difference between a named constant and

a variable is that the value of the named constant does not change during the program.

Like variables, a named constant is given a name and has to be declared in the declaration

part. Actually, the declaration part is divided into two sections, CONST and VAR;

the CONST section comes before the VAR section. Suppose that you would like to use

the value 3.14159 (a numerical constant known as Pi) many times in your calculations.

It would be more convenient to give it a name and use the name in your code. You can

declare named constants as in the following example:

CONST

Pi = 3.14159;

ThisYear = 1992;

Department= 'OtoRhinoLaryngology';

Some constants are predefined in Pascal as standard identifiers. One useful predefined

named constant is MAXINT, which gives the maximum value an integer can possess.

The value depends on the computer used. If you want to know the value of MAXINT

in your computer, use the statement:

WRITELN(MAXINT);

A typical value is 32,767 (two bytes).

In the following program, the perimeter of a circle is calculated using the named constant

Pi.

{ — — — — — — — — — — — — — — — - Example 1-9 — — — — — — — — — — — — — — - }

PROGRAM Constants(OUTPUT);

{ Constant Declarations }

CONST

Pi = 3.14159;

{ Variable Declarations }

VAR

Radius, Perimeter :REAL;

{ Program Block }

BEGIN

Radius := 4.9;

Perimeter := 2 * Pi * Radius;

WRITELN('Perimeter=', Perimeter)

END.

The output of this program is:

Perimeter= 3.0787582000E+01

NOTE If you are using Delphi or Turbo Pascal, you do not need to redefine the

constant Pi, as it is predefined as a standard identifier.

Type Conversion: ROUND, TRUNC

You can assign an integer to a variable of the type REAL, but the opposite is not permitted.

The reason for this is because the storage size allocated for an integer is

smaller than that allocated for a real number. If this were permitted, data could be lost

or corrupted when a large number was moved to a smaller location in which it did not

fit. You can, however, perform the conversion with one of the following two functions:

ROUND(n) rounds n to the closest integer

TRUNC(n) truncates the fraction part of n

where n is a real variable or expression.

Consider these examples:

ROUND(8.4) returns 8

ROUND(8.5) returns 9

TRUNC(8.4) returns 8

TRUNC(8.5) returns 8

As you can see in the examples, the two functions may or may not return the same

integer value for the same argument.

In the following program the two functions are used to get the rounded and the truncated

integer values of the real variable Perimeter

{ — — — — — — — — — — — — — — — Example 1-10 — — — — — — — — — — — — — — - }

PROGRAM Functions1(OUTPUT);

{ Constant Declarations }

CONST

Pi = 3.14159;

{ Variable Declarations }

VAR

Perimeter, Radius :REAL;

RoundedPerimeter, TruncatedPerimeter :INTEGER;

{ Program Block }

BEGIN

Radius := 4.9;

Perimeter := 2*Pi*Radius;

RoundedPerimeter := ROUND(Perimeter);

TruncatedPerimeter := TRUNC(Perimeter);

WRITELN('Perimeter=', Perimeter);

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил