Techniques of Using Variables
|
The Type Definition
|
Some of the variables we will use in Object Pascal can be simplified and
reduced to a new one-word data type. Object Pascal allows you to define
a new data type using the type data type. In this case, type does not
create a new kind of data type; instead, it allows you rename an
existing data as you see fit and use the new type in your program. The
syntax of customizing the name of an existing data type is:
type NewName = KnownDataType;
The type keyword is required to let the compiler know that you are
defining a new data type. The NewName must be a valid name for an Object
Pascal object. The KnownDataType is any of those we have seen so far.
Here is an example:
type NaturalNumber = Integer;
In this case, NaturalNumber becomes a name for
an Integer data type. After this, NaturalNumber can be used exactly as
if you were using an Integer. Here is an example that redefines an
integer data type:
type NaturalNumber = Integer; var NumberOfStudents: NaturalNumber; |
Constants
|
A constant is a value that does not change. There are three main
categories of constants you will be using in your programs: those that
already exist, those built in the Object Pascal language, and those you
will create yourself.
The algebraic numbers you have used in the past are constants because they never change. Examples of constant numbers are 12, 0, 1505, or 88146. Therefore, any number you can think of is a constant. Every letter of the alphabet is a constant and is always the same. Examples of constant letters are d, n, c. Some characters on your keyboard represent symbols that are neither letters nor digits. These are constants too. Examples are #, &, |, !. To simply display a constant on a console window, you can type it in the parentheses of the Write or Writeln procedure. Here is an example:
The algebraic numbers you have used in the past are constants because they never change. Examples of constant numbers are 12, 0, 1505, or 88146. Therefore, any number you can think of is a constant. Every letter of the alphabet is a constant and is always the same. Examples of constant letters are d, n, c. Some characters on your keyboard represent symbols that are neither letters nor digits. These are constants too. Examples are #, &, |, !. To simply display a constant on a console window, you can type it in the parentheses of the Write or Writeln procedure. Here is an example:
program Greeting; {$APPTYPE CONSOLE} begin Writeln('a'); Writeln(248); Write('Press any key to continue...'); Readln; end.
This would produce:
a 248 Press any key to continue...
The Object Pascal language defines some constants that are part of the language. Examples are True, False, or nil.
Some values would be constant by default, but their constancy sometimes depends on the programmer. For example, one programmer can define a constant PI as 3.14. Another programmer can decide that the constant PI would be 3.14159. Therefore, you will be defining your own constant values as you see fit for the goal you are trying to achieve.
The safest technique of using a constant is to give it a name. This allows you to manage it from one standpoint. For example, if you plan to use a number such as 3.14 that represents PI, you can simply use the constant 3.14. Imagine you want to use 3.14 in various sections of the program such as in different functions. If you decide to change the number from 3.14 to 3.14159 or another value, you would have to find every mention of 3.14; this can lead to a programming error. The alternative is to declare a variable and assign it the desired value. The new and defined value is called a constant.
To define a constant, type the const keyword following by a name for the constant. Although in the next lesson we will learn about operators, a constant must be initialized before it can be used. Therefore, a constant should be defined as
Some values would be constant by default, but their constancy sometimes depends on the programmer. For example, one programmer can define a constant PI as 3.14. Another programmer can decide that the constant PI would be 3.14159. Therefore, you will be defining your own constant values as you see fit for the goal you are trying to achieve.
The safest technique of using a constant is to give it a name. This allows you to manage it from one standpoint. For example, if you plan to use a number such as 3.14 that represents PI, you can simply use the constant 3.14. Imagine you want to use 3.14 in various sections of the program such as in different functions. If you decide to change the number from 3.14 to 3.14159 or another value, you would have to find every mention of 3.14; this can lead to a programming error. The alternative is to declare a variable and assign it the desired value. The new and defined value is called a constant.
To define a constant, type the const keyword following by a name for the constant. Although in the next lesson we will learn about operators, a constant must be initialized before it can be used. Therefore, a constant should be defined as
const ConstantName = DesiredValue;The const keyword is required to inform the compiler that you are creating a constant. The ConstantName specifies the name of the constant value. It follows the rules we have applied for the names of variables. The DesiredValue is the value that the constant will hold.
A constant can be initialized with any normal value or an expression. The compiler will find out what type of value the constant holds. Once a constant has been defined, it can be used in the program where its name would be substituted for its value.
No comments:
Post a Comment