TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 20 TURBO-LESSON 5: INTEGER EXPRESSIONS OBJECTIVES - In lesson 5 you will learn about: 1. Assignment statements 2. Integer expressions 3. Problems with expressions 1. Assignment statements. A large part of computer processing is accomplished by storing numbers, strings, and other data objects in memory locations in the computer. There are several ways to store a value in memory in Pascal: (1) use an Input statement (ReadLn), (2) use an Assignment statement, (3) use a Procedure or function. The Assignment statement has the following form: Variable := Expression; An example: A := B + C; The way it works: The computer evaluates the expression on the right side of the replacement operator, :=, and stores the resulting value in the memory location named by the variable on the left. In the example above, the computer would obtain whatever value is currently stored in the memory location called B, add that value to the value it finds in the memory location called C, and store the sum in the memory location, A. If B holds the value 3, and C holds the value 4, then 7 would be stored in the memory location called A. ##### DO: Use the editor to enter the following short program (omit the comments, if you like): PROGRAM ABC; VAR A, B, C : Integer; BEGIN A := 6; {Assign the value 6 to the memory location, A} B:=7; {Assign 7 to B } C := A + B; {Add the values in A and B, Store in C } WriteLn ('A=',A, ' B=',B, ' C=',C); END. Run the program. Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 21 Note that you have just written a complete PASCAL program - from here on, you will just be adding new features in each lesson to enable you to write more complicated programs. (It didn't run? Use the error message to correct any errors. If the error message doesn't make sense, try looking for misplaced or omitted semicolons. Also note that the assignment statement uses the compound symbol, ":=" and not "=".). 2. Integer Expressions. Integer expressions are composed of integer variables, constants, and operators. The operators used with integer expressions are: +, -, *, div, mod. The + and - have their usual meaning, addition and subtraction. The * indicates multiplication. Division of integer numbers is done with div and mod. ##### DO: Examine at PROG5. Two numbers, which you enter, are added, subtracted, and one is cubed using the multiply operator, "*". WriteLn is used to print out the results. Notice that expressions may be calculated in an assignment statement, I_Cubed := I * I * I; or calculated in a WriteLn statement, WriteLn('I - J', I - J); ##### DO: Run the program. Note that the ReadLn statement will attempt to read 2 values. The values should be typed with a space between, not a comma. Enter the values 2 and 5. Check all the results. Are they all correct? ##### DO: Run the program again, this time entering -3 and 5. Is everything correct again? Notice the negative cube of -3 is as expected, -27. (Some incorrect negative cubes will appear a little later in this lesson.) Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 22 The division operators, div and mod are not used in PROG5. To see how they work, ##### DO: Just before the END of PROG5, edit in the following statements: WriteLn('I div J = ', I div J); WriteLn('I mod J = ', I mod J); Run the program, entering the values 5 and 3. Did you get the results expected? Is 5 divided by 3 really 1 and not 1.666? ##### DO: Add these two statements at the end of the program: WriteLn(I,' divided by ', J, ' = ', I div J); WriteLn(' with a remainder of ', I mod J); Run the program with the values 5 and 3. Often, when working with integers, it is useful to know one or both of these components of the division. If you really want the decimal result of the division, the slash, /, could be used with integers. ##### DO: Add this statement at the end of the program: WriteLn('I / J = ', I/J); Run the program with the values 5 and 3. Note the result of division using the slash is the usual result. ##### DO: Before going on, try adding a few WriteLn statements to the program to improve the readability of the output. Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 23 3. Problems with expressions. You should be aware of the possibilities for various types of errors involving expressions. First, an easy to detect error. ##### DO: Declare K to be an integer variable. Add these statements at the end of the program: K := I / J; WriteLn('K = ', K); Run the program. How did it go? This error, "Type mismatch", is easy to find, since the compiler finds it. The reason for the type mismatch is that the result of the division using the "/" is a real number (covered in a later lesson). The variable, K, is an integer. A real number can't be stored in an integer memory location. (Why not? One reason: a real number takes 3 times as much memory space.) A second type of error, is illustrated in the following: ##### DO: Run the program using the values 31 and 5. Check the results of the cube of I. Is it correct? O.K., but notice the cube of I, 29791, is approaching the upper limit of integer variables, 32767. What will happen if you enter 32 and 5? (The correct cube of 32 is 32768, just 1 too large to fit as an integer.) Try it! ##### DO: Run the program using the values 32 and 5. The computer, known for its reliability, informs you: The cube of I = -32768 Now, you know the sign is wrong - positive numbers do not produce negative cubes. But look at the number, -32768. Correct number with the wrong sign? Œ TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 24 ##### DO: Run the program again with the values 33 and 5. The cube of I = -29599 Wrong number! Wrong sign! So why does the computer go merrily on its way - giving you these wrong answers? The computer is very good at detecting errors in the format of program statements, missing declarations, wrong punctuation. There are other types of errors that are more difficult to detect. It is up to you, the programmer, to find ways to keep these errors from going unnoticed. For now, you need to be aware that these problems can occur. Error detection will be covered in later lessons. Œ