In pascal, all variables must be explicitly declared. A variable is declared using the following format : var variable_name : variable type; var counter : integer; Declares an integer variable names counter var initial : char; Declares a variable of type char, a single character var name : string; Declares a variable of type string, a sequence of characters, from 0 to 255 characters in length. var taxrate : real; Declares a variable called taxrate which is a real number var full : boolean; Declares a variable called full which is a boolean value (true or false) Pascal has a number of different simple data types which are enumerated below : Data Type Length Description Char 1 byte Stores a single character, e.g. 'A' or 'X' Integer 16 bits Stores integer values in the range -32768 to +32767 Real 6 bytes Stores real numbers in the range 2.9e-39 to 3.4e38 Boolean 1 bit Stores a TRUE or FALSE value String 1 byte * length(string) Stores a string of characters from 0 to 255 characters long. There are variations on these datatypes, e.g. single, double and extended for real number storage which : allocate more or less memory to contain the value in the variable being declared increase or decrease the range which the variable may hold, or, change the range that may be held in the variable to positive values only. We need not concern ourselves with these extensions of the fundamental data types at this stage. Below is a trivial example of the use of these datatypes in a Pascal program : program vardec1; var letter : char; var number : integer; var real_number : real; var char_string : string; begin letter := 'A'; number := 10; real_number := 214.83; char_string := 'Hello World '; writeln ('The character in variable letter is ', letter); writeln ('The next character after letter is ', succ( letter )); writeln ('The integer in variable number is ', number); writeln ('Variable number less 1 is ', number - 1); writeln ('The real number in variable real_number is ', real_number:7:3); writeln ('Twice the variable real_number is ', 2 * real_number:7:3); writeln ('The string data in the variable char_string is : ',char_string); letter := 'X'; writeln ('We have replaced the character in the variable letter with ', letter); end. There are a few functions and operators which we use in this program which we have not yet come across : The assignment operator :=. After the begin statement, we assign values to the variables we have declared using the assignment operator. Unlike many other programming languages, pascal does not use the equals sign, but a colon equals := for assignment. The equals sign alone is used for comparison only. The precision formatting specifiers when printing numbers, e.g. writeln (real_number:7:3); This specifies that real_number should be printed 7 digits wide with three digits after the decimal point. If we do not use precision specifiers real numbers will be printed with a default number of decimal places which can beunweildy. We calculate what is the next letter in sequence after variable letter with the statement succ (letter). The function succ() returns the value of the data type of the argument it is passed next in succession to the value of the variable. It returns data of the same type as the argument. In this case, it returns the value 'B', the succeeding value to the character 'A'. An alternative method to achieve the same end would be to us the function chr( ord( letter) + 1). In this case, letter is our variable, ord(letter) is the ascii numerical code for that letter, to which we add 1 to get the ascii code for the next letter in sequence. chr(ascii_number_code) returns the character represented by the numerical code ascii_number_code. The end result is the same, but succ() achieves the end much more neatly. Printing multiple variables in a single output string Frequently we will want to make use of many variables in a single output string. This is easily done using the same format as outputting a single variable : Writeln ('The letter is ',letter,' the number is ',number,' and the real number is ', real_number:7:3); To make the command more readable (lack of readability is a significant problem in program maintenance), it is common to break a command such as this over a number of lines. Since the pascal command does not terminate until the semicolon, this can be done as follows : Writeln ('The letter is ',letter, ' the number is ',number, ' and the real number is ', real_number:7:3); Even though, in this case, the comand is broken up over a few lines, the output when printed will display on a single line, in the same way as if the command had been written on a single line. Alternatively the output required could be programmed using a series of write commands. A write command operates in the same way as the writeln command, except that it does not insert a newline character at the end of the text it is printing. In this case, we would use a series of write commands, and the final command would be a writeln command to ensure a new line was 'thrown' at the end of the whole sentence, e.g. Write ('The letter is', letter); Write (' the number is ', number); Writeln (' and the real number is ',real_number:7:3); String Variables In the sample program above we used a variable of type string to print out the text 'Hello World' to the screen. A string variable can contain anything from zero to 255 characters, and can be declared as a fixed character length or as an unspecified character length, e.g. : Var name : string; Var address : string[30]; The first declaration declares the variable name to be of an unspecified length, the second declaration specifies the variable address to be, at most, of length 30 characters. The exact length of a string variable can be ascertained using the length function. Assuming both name and address were defined as above, we list below some string commands and the result that would be expected from executing those commands. Command Outcome Name := 'Mary'; Assigns the name 'Mary' to the variable name Length (name); Returns 4 Name :=''; Returns 0 Address := '30 Woodlands Terrace London'; Assigns the text '30 Woodlands Terrace London' to the variable Address Length (address); Returns 26; Address := '30 Woodlands Terrace, Essex Road, Docklands, London E4 1A3'); Assigns the text '30 Woodlands Terrace, Essex Ro' to the variable Address, as Address is limited to 30 characters long Length (address); Returns 30; This output is illustrated in the following program : program vardec2; var name : string; var address : string[30]; begin name := 'Mary'; writeln ('Variable name contains : ',name,' and is of length ',length(name)); address := '30 Woodlands Terrace London'; writeln ('Variable address contains : ',address,' and is of length ',length(address)); address := '30 Woodlands Terrace, Essex Road, Docklands, London E4 1A3'; writeln ('Variable address contains : ',address,' and is of length ',length(address)); end. It is important to note that, as in the case of the second address above, a value can be assigned to a string which is longer than the string variable can accommodate. If this is the case, the compiler will not throw up an error message, but will truncate the value at the length of the declared string. Constant Declarations In the examples above, we have assigned values to variables and changed the values that those variables have contained as we wanted. However it can be useful to declare an object and not to allow the value of that object to be changed. In this case we are declaring a constant rather than a variable. The declaration follows a similar format as for declaring a variable, except we do not need to specify the type and we use the equals rather than the assignment sign, e.g. : Const max_accounts = 100; Const report_header = ('ACME Company Accounts'); Because a constant cannot have a value assigned to it in the code, it must be assigned a value in the declaration. An attempt to assign a value to an object declared as const will result in an error. Once declared and assigned a constant can be referenced (but not re-assigned) in the same way as a corresponding variable, e.g. : Writeln (report_header); Writeln ('Our system allows for holding a maximum of ',max_accounts,' accounts);
Hosted by www.Geocities.ws

1