Chapter 3 Con't

Pascal Tutorial - Chapter 3

SIMPLE DATA TYPES

WHAT IS A DATA TYPE? A type in Pascal, and in several other popular programming languages, defines a variable in such a way that it defines a range of values which the variable is capable of storing, and it also defines a set of operations that are permissible to be performed on variables of that type. TURBO Pascal has eight basic data types which are predefined and can be used anywhere in a program provided you use them properly. This chapter is devoted to illustrating the use of these eight data types by defining the allowable range of values that can be assigned to them, and by illustrating the operations that can be done to variables of these types. The eight 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 shortint The integers from -128 to 127 word     The integers from 0 to 65535 longint  The integers from -2147483648 to 2147483647 Please note that four of these types of data (char, shortint, word, and longint) are not a part of the standard Pascal definition but are included as extensions to the TURBO Pascal compiler. In addition to the above data types TURBO Pascal version 5.0 and later have the following data types available; 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 TURBO Pascal version 5.0 and newer have these four types available which use the 80X87 math coprocessor. Because TURBO Pascal has a software emulator for the floating point operations, an 80X87 math coprocessor is not absolutely required to use these new types with these versions. Of course, your resulting program will run much faster if you have the coprocessor available for use by the program. Note that the math coprocessor is built into every 80486 and Pentium processor. A complete definition of the available types for each compiler can be found in your TURBO Pascal reference manual. 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. OUR FIRST VARIABLES Example program --> INTVAR.PAS 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.PAS into your Pascal system and let's take a look at it. 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. 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 use of the reserved word var, but it is used to define three different variables, Count, X, and Y. Once the word 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, and is therefore different from a reserved word. A standard 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. 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, and prevent 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 is 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. 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 another use in Pascal. The first three statements give X the value of 12, Y the value of 13, and Count the value of 12 + 13 or 25. In order to get those values out of the computer, we need another extension to the Writeln statement. The first part of the data within the parentheses should be very familiar to you 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.PAS and examine its output. Example program --> INTVAR2.PAS To illustrate the various ways to output data, load INTVAR2.PAS and observe that even though the output is identical, it is output in a completely different manner. The Writeln statements are broken into pieces and the individual pieces are output with Write and Writeln statements. 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 after you are certain that the two programs are actually identical. NOW LET'S USE LOTS OF VARIABLES Example program --> ALLVAR.PAS Load ALLVAR.PAS to observe a short program using five of the basic data types. The variables are assigned values and the values are printed. A complete and detailed description of the options available in the Write statement is given in your TURBO Pascal reference manual. Check the index to find this information for the version you are using. It would be to your advantage to read this section now 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 in a later chapter of this tutorial. 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 several conversions of this kind will be given in the example program named CONVERT.PAS later in this chapter. Example program --> REALMATH.PAS Since we have some variables defined, it would be nice to use the properties of computers for which they are famous, namely some arithmetic. Two programs are available for your observation to illustrate the various kinds of math available, REALMATH.PAS using real variables, and INTMATH.PAS using integer variables. You can edit, compile, and run these on your own with no further comment from me except the comments embedded into the source files. You should output some of the results using the method of outputting illustrated in the previous example program. Read the definition of how to do this in your TURBO Pascal User's Guide. Example program --> INTMATH.PAS The example program named INTMATH.PAS illustrates some of the math capabilities of Pascal when using integer type variables. A byte type variable is used just like an integer variable but with a much smaller allowable range. 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 Example program --> BOOLMATH.PAS Let's take a look at a boolean type 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. A complete list of the relational operators available with Pascal is given in the following list. =    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, and they can be used to compare boolean variables. An illustration is the best way to learn about the boolean variable so load BOOLMATH.PAS and observe it. In BOOLMATH.PAS 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 boolean 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 value to the variable with the big name, the values are all printed out. Note that if either A or B is TRUE, the result is TRUE in line 18. 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 both of two things are true, in which case you will use the reserved word and with two boolean expressions. If both 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 and inverts the sense of the boolean variable D. 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. This should indicate the level of flexibility available with a boolean variable. Compile and run this program, then add some additional printout to see if the boolean variables change in the manner you think they should in the last few statements. SHORT CIRCUIT OR COMPLETE EVALUATION? Suppose you have several boolean expressions "and"ed together, and when evaluation starts, the first expression results in a FALSE. Since the first expression is FALSE, it is impossible for the following expressions to ever allow the final result to be TRUE because the first FALSE will force the answer to be FALSE. It seems like a waste of execution time to continue evaluating terms if the final result is already known, but that is exactly what standard Pascal will do because of the language definition. This is known as complete evaluation of a boolean expression. If the system is smart enough to realize that the final result is known, it could stop evaluation as soon as the final result is known. This is known as short circuit evaluation of a boolean expression, and could also be applied if a term of an "or"ed boolean expression resulted in a TRUE, since the result would always be TRUE. TURBO Pascal versions 5.0 and later, allows you to choose between complete evaluation or short circuit evaluation. The default for these compilers is the short circuit form but it can be changed through the Options menu when you are using the integrated environment, or through use of a compiler directive. LET'S LOOK AT THE CHAR TYPE VARIABLE Example program --> CHARDEMO.PAS 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.PAS is included to give you an idea of how a char type variable can be used. Study, then compile and run CHARDEMO.PAS for a very brief idea of what the char type variable is used for. Example program --> CONVERT.PAS Examine the sample program CONVERT.PAS for several examples of converting data from one simple variable to another. The comments make the program self explanatory except for the strings which we will study in chapter 7 of this tutorial. EXTENDED INTEGER TYPES Example program --> EXTINT.PAS Display the program EXTINT.PAS for an example of using the extended integer types available with the Pascal 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. EXTENDED REAL TYPES Example Program --> EXTREAL.PAS Display the program EXTREAL.PAS for an example using the new "real" types available with the newer versions of TURBO Pascal. If you are using a version of TURBO Pascal which is 5.0 or newer, you can use the 80X87 math coprocessor. 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 again, you should keep in mind that use of the larger "real" types costs you extra storage space and reduced run-time speed, but gives you more accuracy.

PROGRAMMING EXERCISE 1.	Write a program containing several variable definitions and do some math on them, printing out the results.

Chapter 4 Con't

Home