Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Constants"

From Lazarus wiki
Jump to navigationJump to search
(New page: 1C - Constants (author: Tao Yue, state: unchanged) Constants are referenced by identifiers, and can be assigned one value at the beginning of the program. The value stored in a constant c...)
 
Line 4: Line 4:
  
 
Constants are defined in the constant section of the program:
 
Constants are defined in the constant section of the program:
 +
<delphi>
 
   1: const
 
   1: const
 
   2:  Identifier1 = value;
 
   2:  Identifier1 = value;
 
   3:  Identifier2 = value;
 
   3:  Identifier2 = value;
 
   4:  Identifier3 = value;
 
   4:  Identifier3 = value;
 
+
</delphi>
 
For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans. These data types will be further explained in the next section.
 
For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans. These data types will be further explained in the next section.
<font color="#000000">  1: </font><font color="#006699"><strong>const</strong></font>
+
<delphi>
<font color="#990066">  2:   </font><font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>=</strong></font> <font color="#ff00cc">'</font><font color="#ff00cc">Tao</font><font color="#ff00cc"> </font><font color="#ff00cc">Yue</font><font color="#ff00cc">'</font><font color="#000000"><strong>;</strong></font>
+
const
<font color="#000000">  3:   </font>FirstLetter <font color="#000000"><strong>=</strong></font> <font color="#ff00cc">'</font><font color="#ff00cc">a</font><font color="#ff00cc">'</font><font color="#000000"><strong>;</strong></font>
+
   Name = 'Tao Yue';
<font color="#000000">  4:   </font>Year <font color="#000000"><strong>=</strong></font> <font color="#ff0000">1997</font><font color="#000000"><strong>;</strong></font>
+
   FirstLetter = 'a';
<font color="#000000">  5:   </font>pi <font color="#000000"><strong>=</strong></font> <font color="#ff0000">3</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">1415926535897932</font><font color="#000000"><strong>;</strong></font>
+
   Year = 1997;
<font color="#000000">  6:   </font>UsingNCSAMosaic <font color="#000000"><strong>=</strong></font> <font color="#cc00cc">TRUE</font><font color="#000000"><strong>;</strong></font>
+
   pi = 3.1415926535897932;
 
+
   UsingNCSAMosaic = TRUE;
 
+
</delphi>
 
Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts with newer languages which often use or allow double quotes or Heredoc notation. Standard Pascal does not use or allow double quotes to mark characters or strings.
 
Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts with newer languages which often use or allow double quotes or Heredoc notation. Standard Pascal does not use or allow double quotes to mark characters or strings.
  
Line 23: Line 24:
  
 
Typed constants force a constant to be of a particular data type. For example,
 
Typed constants force a constant to be of a particular data type. For example,
<font color="#000000">  1: </font><font color="#006699"><strong>const</strong></font>
+
<delphi>
<font color="#000000">  2: </font>  a <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font> <font color="#000000"><strong>=</strong></font> <font color="#ff0000">12</font><font color="#000000"><strong>;</strong></font>
+
const
 +
  a : real = 12;
 +
</delphi>
  
 
would yield an identifier a which contains a real value 12.0 instead of the integer value 12.
 
would yield an identifier a which contains a real value 12.0 instead of the integer value 12.

Revision as of 15:42, 5 January 2010

1C - Constants (author: Tao Yue, state: unchanged)

Constants are referenced by identifiers, and can be assigned one value at the beginning of the program. The value stored in a constant cannot be changed.

Constants are defined in the constant section of the program: <delphi>

  1: const
  2:   Identifier1 = value;
  3:   Identifier2 = value;
  4:   Identifier3 = value;

</delphi> For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans. These data types will be further explained in the next section. <delphi> const

 Name = 'Tao Yue';
 FirstLetter = 'a';
 Year = 1997;
 pi = 3.1415926535897932;
 UsingNCSAMosaic = TRUE;

</delphi> Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts with newer languages which often use or allow double quotes or Heredoc notation. Standard Pascal does not use or allow double quotes to mark characters or strings.

Constants are useful for defining a value which is used throughout your program but may change in the future. Instead of changing every instance of the value, you can change just the constant definition.

Typed constants force a constant to be of a particular data type. For example, <delphi> const

 a : real = 12;

</delphi>

would yield an identifier a which contains a real value 12.0 instead of the integer value 12.

previous contents next