|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
2-5 The STRING TypeActually, you will never need to use the PACKED ARRAY OF CHAR unless you are using one of the old implementations of Pascal on a mainframe computer. In the modern implementations (such as Turbo and UCSD), the type STRING is defined. Declaration of a STRINGYou can declare a variable of the type STRING, as in this example: VAR StudentName: STRING; This declaration lets you store a string of up to a certain size in the variable StudentName. Although the maximum length of the string variable is 255 in Turbo (80 in UCSD), the actual length (also referred to as dynamic length) of the string is the number of stored characters. You can declare the string variable and its maximum length in the same statement: VAR StudentName: STRING[20]; In this case the maximum length of a string stored in the variable StudentName is 20 characters. Look at this program, which reads a name of a maximum length of 20 characters and displays it on the screen. { ------------------------------ figure 2-8 ------------------------------ } { String Type in Turbo Pascal } PROGRAM StringDemo(INPUT,OUTPUT); VAR Name:STRING[20]; BEGIN WRITE('Please enter a name of 20 characters or less:'); READLN(Name); WRITELN('The name you entered is ',Name, '. Is that right?') END. A sample run of the program gives the following: Please enter a name of 20 characters or less:Peter Rigby The name you entered is Peter Rigby. Is that right? Note that if you assign a string constant of more than 20 characters to the variable Name, the extra characters will be truncated. The LENGTH of a STRINGYou can measure the dynamic length of a string using the function LENGTH. If you want, for instance, to measure the length of the string Name in the last program, you may use the expression:
If you display the value of this expression, you get the exact number of characters contained in the string variable, including the spaces. If the string variable is empty, the dynamic length is zero. In the following program, you enter a name and the program displays the actual length both before and after the variable assignment. { ------------------------------ figure 2-9 ------------------------------ } { Dynamic Length of a String } PROGRAM StringLen(INPUT,OUTPUT); VAR Name:STRING[20]; BEGIN WRITELN('The dynamic length of the string is now ',LENGTH(Name), 'characters'); WRITE('Please enter a name of 20 characters or less:'); READLN(Name); WRITELN('The dynamic length of the string is now ',LENGTH(Name), 'characters') END. The following is a sample run: The dynamic length of the string is now 0 characters Please enter a name of 20 characters or less:Dale Sanders The dynamic length of the string is now 12 characters The introduction of the type STRING in Pascal filled a gap and added a powerful tool, especially in the field of text processing. More on string functions and operations later. 2-6 The BOOLEAN TypeThe BOOLEAN values (sometimes called logical values) are the two constants:
They are named after the English mathematician George Boole (1815-1864). In Pascal you can declare a variable of the type BOOLEAN, which may only hold one of the two BOOLEAN constants TRUE or FALSE, as in the following example: VAR Result: BOOLEAN; Simple BOOLEAN ExpressionsYou can assign a boolean constant to a boolean variable, such as: Result:= TRUE; You may also assign a boolean expression to a variable such as: Result:= A > B; If A, for example, holds the value 22.5 and B holds the value 2.3, then the expression A > B (A is larger than B) is evaluated as TRUE. If A holds 1.8, then the condition is not satisfied and the expression is evaluated as FALSE. You can build boolean expressions using the relational operators shown in Table 2-4.
Relational operators are used with any data type: numeric, character, or BOOLEAN. Here are some examples:
For characters, an expression such as: 'A' < 'B' is always TRUE, because the letter A comes before B in the alphabet; in other words, it has a smaller ordinal number. Using the same logic, the following expressions are TRUE: '9' > '1' 'Y' < 'Z' The following program reads from the keyboard the value of two integers A and B and displays the value of the boolean expression A = B. { ------------------------------ figure 2-10 ----------------------------- } { Boolean Variables } PROGRAM Compare1(INPUT,OUTPUT); VAR A, B :INTEGER; Result:BOOLEAN; BEGIN WRITE('Please enter two integers: '); READLN(A, B); Result:= (A = B); { or, Result:= A = B; The parentheses are not necessary. } WRITELN('The comparison is ', Result) END. The following are two sample runs of the program: Please enter two integers: 5 5 The comparison is TRUE Please enter two integers: 50 55 The comparison is FALSE
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |