CHAPTER 3 - The simple Pascal data types TURBO Pascal has 5 basic data types which are predefined and can be used anywhere in a program provided you use them properly. The five types and a very brief description follows; integer Whole numbers from -32768 to 32767 byte The integers from 0 to 255 real Floating point numbers from 1E-38 to 1E+38 boolean Can only have the value TRUE or FALSE char Any character in the ASCII character set Please note that the byte type of data is not a part of the standard Pascal definition but is included as an extension to the TURBO Pascal compiler. TURBO Pascal version 4.0 has three additional "integer" types available which are not available with version 3.0, and they are defined as follows; shortint The integers from -128 to 127 word The integers from 0 to 65535 longint The integers from -2147483648 to 2147483647 In addition to the above data types TURBO Pascal version 4.0 has the following data types available but in order to use them, you must have an 80X87 math coprocessor installed in your system; single Real type with 7 significant digits double Real type with 15 significant digits extended Real type with 19 significant digits comp The integers from about -10E18 to 10E18 A complete definition of the available types for each compiler can be found on pages 41 and 42 of the TURBO Pascal version 3.0 reference manual, and on pages 39 through 44 of the reference manual for version 4.0. It would be good to read these pages now for a good definition prior to learning how to define and use them in a program. Note that all of these will be used in example programs in this chapter. The integers are by far the easiest to understand so we will start with a simple program that uses some integers in a very simple way. Load INTVAR into your TURBO system and lets take a look at it. OUR FIRST VARIABLES Immediately following the program statement is another reserved word, "var". This reserved word is used to define a variable before it can be used anywhere in the program. Page 13 CHAPTER 3 - The simple Pascal data types There is an unbroken rule of Pascal that states "Nothing can be used until it is defined." The compiler will complain by indicating a compilation error if you try to use a variable without properly defining it. It seems a bit bothersome to have to define every variable prior to its use, but this rule will catch many spelling errors of variables before they cause trouble. Some other languages will simply define a new variable with the new name and go merrily on its way producing some well formatted garbage for you. Notice that there is only one "var", but it is used to define three different variables, Count, X, and Y. Once a var is recognized, the compiler will continue to recognize variable definitions line after line until it finds another reserved word. It would be permissible to put a var on the second line also but it is not necessary. It would also be permissible to put all three variables on one line but your particular programming style will dictate where you put the three variables. Following the colon on each line is the word "integer" which is a standard identifier which is different from a reserved word. An identifier is predefined like a reserved word but you can redefine it thereby losing its original purpose and meaning. For now and for a long time, don't do that. Page 38 contains a list of standard identifiers in TURBO Pascal 3.0. There is no corresponding list in the reference manual for TURBO Pascal 4.0. OUR FIRST ARITHMETIC Now that we have three variables defined as integer type variables, we are free to use them in a program in any way we desire as long as we use them properly. If we tried to assign a real value to X, the compiler will generate an error, once again preventing a garbage output. Observe the start of the main body of the program. There are three statements assigning values to X, Y, and Count. A fine point of mathematics would state that Count is only equal to the value of X+Y until one of them was modified, therefore the equal sign used in so many other languages is not used here. The sign := is used, and can be read as "is replaced by the value of", when reading a listing to preserve the mathematical purity of Pascal. Another quicker way is to use the word "gets". Thus X := X + 1 would be read "X gets the value of X plus 1". We will see later that the simple equal sign is reserved for use in a different manner. The first three statements give X the value of 12, Y the value of 13, and Count the value of 12+13 or 25. We need to get those values out of the computer, so we need another extension to the Writeln statement. The first part of the data within the parentheses should be familiar to you Page 14 CHAPTER 3 - The simple Pascal data types now, but the second part is new. Multiple outputs can be handled within one Writeln if the fields are separated by a comma. To output a variable, simply write the variable's name in the output field. The number following the variable in each case is the number of output columns to be used by the output data. This number is optional and can be omitted allowing the system to use as many columns as it needs. For purposes of illustration, they have all been assigned different numbers of columns. At this point, you can compile and run INTVAR and examine its output. To illustrate the various ways to output data, load INTVAR2 and observe that even though the output is identical, it is output in a completely different manner. Observe especially that a Writeln all by itself simply moves the cursor to the beginning of a new line on the video monitor. Compile and run this program and observe its output. NOW LET'S USE LOTS OF VARIABLES Load ALLVAR to observe a short program using all 5 of the basic data types. The variables are simply assigned values and the values are printed. A complete and detailed description of the options available in the Write statement is given in the TURBO reference manual version 3.0 on pages 111 through 113, and on pages 500 through 502 for version 4.0. It would be to your advantage to read this section at this time since very little explanation will be given about Write statements from this point on. We will discuss the method by which we can write to disk files or other output devices when the time comes. Back to the basic types. Pascal does lots of cross checking for obvious errors. It is illegal to assign the value of any variable with a value that is of the wrong type or outside the allowable range of that variable. There are routines to convert from one system to another when that is necessary. Suppose, for example, that you wished to use the value of an integer in a calculation of real numbers. That is possible by first converting the integer into a real number of the same value and using the new real type variable in the desired calculations. The new real type variable must of course be defined in a var statement as a real type variable before it can be used. Details of how to do the conversion will be given later. Since we have some variables defined, it would be nice to use the properties of computers for which they are famous, namely some mathematics. Two programs are available Page 15 CHAPTER 3 - The simple Pascal data types for your observation to illustrate the various kinds of math available, REALMATH using real variables, and INTMATH using integer variables. You can edit, compile, and run these on your own with no comment from me except the comments embedded into the source files. Chapter 6 on pages 51 to 54 of your version 3.0 TURBO reference manual completely defines the simple mathematics available. The corresponding list for version 4.0 is found in chapter 3 on pages 46 through 49. A byte type variable is used just like an integer variable but with a much smaller value. Only one byte of computer memory is used for each variable defined as a byte type variable, but 2 are used for each integer type variable. BOOLEAN VARIABLES Lets take a look at the boolean variable which is only allowed to take on two different values, TRUE or FALSE. This variable is used for loop controls, end of file indicators or any other TRUE or FALSE conditions in the program. Variables can be compared to determine a boolean value. Following is a complete list of the relational operators available with Pascal. = equal to <> not equal to > greater than < less than >= greater than or equal to <= less than or equal to These operators can be used to compare any of the simple types of data including integer, char, byte, and real type variables or constants. An illustration is the best way to learn about the boolean variable so load BOOLMATH and observe it. In BOOLMATH we define a few boolean variables and two integer type variables for use in the program and begin by assigning values to the two integer variables. The expression "Junk = Who" in line 14 is actually a boolean operation that is not true since the value of Junk is not equal to the value of Who. The result is therefore FALSE and that value is assigned to the boolean variable A. The boolean variable B is assigned the value of TRUE because the expression "Junk = (Who - 1)" is true. The boolean variables C and D are likewise assigned some values in a manner that should not need any comment. After assigning a Page 16 CHAPTER 3 - The simple Pascal data types value to the variable with the big name, the values are all printed out. WHERE DO WE USE THE BOOLEAN VARIABLES? We will find many uses for the boolean type variable when we study the loops and conditional statements soon, but until then we can only learn what they are. Often, in a conditional statement, you will want to do something if either of two things are true, in which case you will use the reserved word "and" with two boolean expressions. If either of the two are true, the result will be true. Line 29 is an example of this. If the boolean variables B, C, and D, are all true, then the result will be true and A will be assigned the value of TRUE. If any one of them is false, the result will be false and A will be assigned the value of FALSE. In Line 31, where the "or" operator is illustrated, if any of the three boolean variables is true, the result will be true, and if all three are false, the result will be false. Another boolean operator is the "not" which is illustrated in line 30. Examine line 33 which says the result is true only if the variable Junk is one less than Who, or if Junk is equal to Who. Compile and run this program, then add some additional printout to see if the boolean variables change the way you think they should in the last few statements. LETS LOOK AT THE CHAR TYPE VARIABLE A char type variable is a very useful variable, but usually not when used alone. It is very powerful when used in an array or some other user defined data structure which is beyond the scope of this chapter. A very simple program, CHARDEMO is included to give you an idea of how a char type variable can be used. Study then compile and run CHARDEMO for a very brief idea of what the char type variable is used for. Examine the sample program CONVERT for several examples of converting data from one simple variable to another. The program is self explanatory. THIS IS FOR TURBO PASCAL 4.0 USERS If you are using TURBO Pascal version 3.0, you are finished with this chapter because the data types illustrated in the last two programs are not available with that compiler. Page 17 CHAPTER 3 - The simple Pascal data types If you are using TURBO Pascal 4.0, display the program NEWINT4 for an example of using the extended integer types available with that compiler. Four variables are defined and values assigned to each, then the results are displayed. When you compile and run the program, you will see that the variable Big_int can indeed handle a rather large number. It must be pointed out that the calculation in lines 13 and 21 result in a different answer even though they appear to be calculating the same thing. An explanation is in order. The quantity named MaxInt used in lines 10 and 13 is a constant built into the system that represents the largest value that an integer type variable can store. On the first page of this chapter we defined that as 32767 and when running the program you will find that Index displays that value as it should. The constant MaxInt has a type that is of a universal_integer type as do all of the numeric constants in line 13. The result then is calculated to the number of significant digits dictated by the left hand side of the assignment statement which is of type longint resulting in a very large number. When we get to line 21, however, the variable Index is of type integer so the calculations are done as though the constants were of type integer also which causes some of the more significant digits to be truncated. The truncated result is converted to type longint and assigned to the variable Big_int and the truncated value is displayed by line 22. After that discussion it should be apparent to you that it is important what types you use for your variables. It must be emphasized that it would not be wise to use all large type variables because they use more storage space and slow down calculations. Experience will dictate the proper data types to use for each application. NOW FOR THE NEW REAL TYPES If you are using TURBO Pascal 4.0, display the program NEWREAL4 for an example using the new "real" types available with version 4.0. Note that you must have an 80X87 math coprocessor installed to compile and run this program. There is a note given in the file to aid you in selecting it for use. This program should be self explanatory so nothing will be said except that when you run it you can observe the relative accuracy of each of the variable types. Once Page 18 CHAPTER 3 - The simple Pascal data types again, you should keep in mind that use of the larger "real" types costs you a bit in storage space and run-time speed. PROGRAMMING EXERCISE 1. Write a program containing several variable definitions and do some math on them, printing out the results. Page 19