Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Variables and Data Types"

From Free Pascal wiki
Jump to navigationJump to search
(New page: 1D - Variables and Data Types (author: Tao Yue, state: unchanged) Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared...)
 
Line 2: Line 2:
  
 
Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used:
 
Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used:
<font color="#000000">  1:</font> <font color="#006699"><strong>var</strong></font>
+
<delphi>
<font color="#000000">  2:</font>   IdentifierList1 <font color="#000000"><strong>:</strong></font> DataType1<font color="#000000"><strong>;</strong></font>
+
var
<font color="#000000">  3:</font>   IdentifierList2 <font color="#000000"><strong>:</strong></font> DataType2<font color="#000000"><strong>;</strong></font>
+
   IdentifierList1 : DataType1;
<font color="#000000">  4:</font>   IdentifierList3 <font color="#000000"><strong>:</strong></font> DataType3<font color="#000000"><strong>;</strong></font>
+
   IdentifierList2 : DataType2;
<font color="#990066">  5:</font>   <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
   IdentifierList3 : DataType3;
 
+
   ...  
 +
</delphi>
 
<tt>IdentifierList</tt> is a series of identifiers, separated by commas (<tt>,</tt>). All identifiers in the list are declared as being of the same data type.
 
<tt>IdentifierList</tt> is a series of identifiers, separated by commas (<tt>,</tt>). All identifiers in the list are declared as being of the same data type.
  
Line 27: Line 28:
  
 
An example of declaring several variables is:
 
An example of declaring several variables is:
<font color="#000000">  1:</font> <font color="#006699"><strong>var</strong></font>
+
<delphi>
<font color="#000000">  2:</font>   age<font color="#000000"><strong>,</strong></font> year<font color="#000000"><strong>,</strong></font> grade <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
var
<font color="#000000">  3:</font>   circumference <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font>
+
   age, year, grade : integer;
<font color="#000000">  4:</font>   LetterGrade <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>char</strong></font><font color="#000000"><strong>;</strong></font>
+
   circumference : real;
<font color="#990066">  5:</font>   DidYouFail <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>boolean</strong></font><font color="#000000"><strong>;</strong></font>
+
   LetterGrade : char;
 +
   DidYouFail : Boolean;
 +
</delphi>
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"

Revision as of 15:45, 5 January 2010

1D - Variables and Data Types (author: Tao Yue, state: unchanged)

Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used: <delphi> var

 IdentifierList1 : DataType1;
 IdentifierList2 : DataType2;
 IdentifierList3 : DataType3;
 ... 

</delphi> IdentifierList is a series of identifiers, separated by commas (,). All identifiers in the list are declared as being of the same data type.

The basic data types in Pascal include:

  • integer
  • real
  • char
  • boolean

Standard Pascal does not make provision for the string data type, but most modern compilers do. Experienced Pascal programmers also use pointers for dynamic memory allocation, objects for object-oriented programming, and many others, but this gets you started.

More information on Pascal data types:

  • The integer data type can contain integers from -32768 to 32767. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values.
  • The real data type has a range from 3.4x10-38 to 3.4x1038, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character E separating the mantissa from the exponent. Thus, 452.13 is the same as 4.5213e2
  • The char data type holds characters. Be sure to enclose them in single quotes, like so: 'a' 'B' '+' Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
  • The boolean data type can have only two values: TRUE and FALSE

An example of declaring several variables is: <delphi> var

 age, year, grade : integer;
 circumference : real;
 LetterGrade : char;
 DidYouFail : Boolean;

</delphi>

previous contents next