Previous Table of Contents Next


5.2.2.2. Writing and Reading from the Screen

This section describes procedures used to read and write to the screen. In Turbo Pascal, they are read, readln, write, and writeln.

Listing 5.2 shows how to use these procedures. read takes a variable from the screen, and write writes a variable to the screen. readln and writeln behave in the same manner as read and write, except they move to the next line. In the example, note that another statement which is not required but which is almost always used is VAR. It denotes the start of a variable declaration section. It is terminated by another declaration identifier, such as begin. The variable named typed_variable is declared as a Pascal type string, which is a phrase of text not greater than 255 characters. Also, comments in Pascal are demonstrated here.

Listing 5.2. A demonstration of reads and writes.

  program fig2;
    var
      typed_variable: string;
    { This denotes a comment, between the braces }
    (* If there are no braces on your keyboard, this will also work *)
    begin
      write(‘Type something to me, and I will repeat it. ’);
      readln(typed_variable);
      writeln(‘You typed: ’, typed_variable, ‘.’);
    end.

In the writeln statement, note that a variable may be displayed along with constant text, separated by commas, as indicated. Strings, when addressed, are to have single quotation marks () surrounding the text. When you want to write a single quotation mark, two of them—rather than a double quotation mark ()—must be placed. A null writeln, as shown in Listing 5.3, produces movement to the next line, whereas a null readln, in a similar manner, pauses the program until Enter is pressed.

Also, another feature of the write procedures involves text placement, which can be observed in Listing 5.3. A colon may be placed after any part of a statement in a write or writeln call. The end of the section of text is placed on the column number indicated. If this is not possible, the text is placed on the screen in a left-justified manner. Included in Listing 5.3 is a number rule, which enables you to observe the result of this option.

Listing 5.3. A demonstration of text placement.

  program fig3;
    begin
      writeln(‘0000000001111111111222222222233333333334444444444
      55555’,
              ‘555556’);
      writeln(‘123456789012345678901234567890
      123456789012345678901234’,
               ‘567890’);
      writeln;
      writeln(‘Hi, I’m happy, are you?’:50);
    end.

5.2.2.3. Arithmetic Manipulation

This section describes standard addition, subtraction, multiplication, division, and concatenation. For purposes of this section, variables declared as real are decimal numbers, such as 3.23, and variables declared as integer are whole numbers, such as 5. Order of operations rules and use of parentheses in Turbo Pascal are consistent with the rules of mathematics. These rules usually are consistent with all programming languages. In Listing 5.4, observe means of doing these arithmetic functions.

There are two additional mathematical functions that exist in Turbo Pascal, referred to as DIV, or integer division, and MOD, or modulo. Both are equivalent to division in the order of operations. DIV returns the integer part of a division between two integers, and MOD returns the remainder of an integer division. Listing 5.4 illustrates these functions.

An assignment of a value to a variable or a value from one variable to another variable is made by the use of the symbol := (note that this is different from = in Pascal). The data types must be similar in order for the assign to work properly. Listing 5.4 also illustrates concatenation of strings by use of the + operator. Also, a valid data identifier must not start with a number or _, and must not have any of the symbols used as a part of Turbo Pascal coding.

Listing 5.4. An illustration of mathematical functions, concatenation, and assigns.

  program fig4;
    { this is not the most efficient program, it has been expanded to }
    { purposely illustrate several ways that these things can be done }
    var
      starting: string;
      input1, input2: integer;
      output: real;
    begin
      { demonstrating concatenation }
      starting := ‘Turbo’ + ‘ Pascal’;
      writeln(starting);
      { demonstrating math }
      input1 := 7;
      input2 := 3;
      writeln(input1, ‘ and ’, input2, ‘:’);
      writeln(‘Add: ’, 7 + 3);
      writeln(‘Subtract: ’, input1 - input2);
      writeln(‘Multiply: ’, input1 * input2);
      { real division }
      write(‘Divide: ’, input1 / input2);
      { integer division, also note that a statement can extend to }
      { multiple lines }
      writeln(‘ or alternatively ’, input1 div input2,
              ‘ with a remainder of ’, input1 mod input2);
    end.


Previous Table of Contents Next