..:: A guide for You who wanna be a programming master!! ::..

Tips 'n Tricks

Program Structure

Materials :
  1. Basics
  2. Input-output
  3. Expressions
  4. Procedures and Functions


Input is what comes into the program. It can be from the keyboard, the mouse, a file on disk, a scanner, a joystick, etc.

We will not get into mouse input in detail, because that syntax differs from machine to machine. In addition, today's event-driven windowing operating systems usually handle mouse input for you.

The basic format for reading in data is:

read (Variable_List);

Variable_List is a series of variable identifiers separated by commas.

read treats input as a stream of characters, with lines separated by a special end-of-line character. readln, on the other hand, will skip to the next line after reading a value, by automatically moving past the next end-of-line character:

readln (Variable_List);

Suppose you had this input from the user, and a, b, c, and d were all integers.

45 97 3
1 2 3

Here are some sample read and readln statements, along with the values read into the appropriate variables.

Statement(s) a b c d
read (a);
read (b);
45 97 0 0
readln (a);
read (b);
45 1 0 0
read (a, b, c, d); 45 97 3 1
readln (a, b);
readln (c, d);
45 97 1 2

When reading in integers, all spaces are skipped until a numeral is found. Then all subsequent numberals are read, until a non-numeric character is reached (including, but not limited to, a space).


When an integer is read from the above input, its value becomes 8352. If, immediately afterwards, you read in a character, the value would be '.' since the read head stopped at the first alphanumeric character.

Suppose you tried to read in two integers. That would not work, because when the computer looks for data to fill the second variable, it sees the '.' and stops since it couldn't find any data to read.

With real values, the computer also skips spaces and then reads as much as can be read. However, many Pascal compilers place one additional restriction: a real that has no whole part must begin with 0. So .678 is invalid, and the computer can't read in a real, but 0.678 is fine.

Make sure that all identifiers in the argument list refer to variables! Constants cannot be assigned a value, and neither can literal values.

back to top


For writing data to the screen, there are also two statements, one of which you've seen already in last chapter's programming assignment:

write (Argument_List);
writeln (Argument_List);

The writeln statement skips to the next line when done.

You can use strings in the argument list, either constants or literal values. If you want to display an apostrophe within a string, use two consecutive apostrophes. Displaying two consecutive apostrophes would then requires you to use four. This use of a special sequence to refer to a special character is called escaping, and allows you to refer to any character even if there is no key for it on the keyboard.

back to top

Formatting Output

Formatting output is quite easy. For each identifier or literal value on the argument list, use:

Value : field_width

The output is right-justified in a field of the specified integer width. If the width is not long enough for the data, the width specification will be ignored and the data will be displayed in its entirety (except for real values — see below).

Suppose we had:

write ('Hi':10, 5:4, 5673:2);

The output would be (that's eight spaces before the Hi and three spaces after):

Hi 55673

For real values, you can use the aforementioned syntax to display scientific notation in a specified field width, or you can convert to fixed decimal-point notation with:

Value : field_width : decimal_field_width

The field width is the total field width, including the decimal part. The whole number part is always displayed fully, so if you have not allocated enough space, it will be displayed anyway. However, if the number of decimal digits exceeds the specified decimal field width, the output will be displayed rounded to the specified number of places (though the variable itself is not changed).

write (573549.56792:20:2);

would look like (with 11 spaces in front):


back to top


Reading from a file instead of the console (keyboard) can be done by:

read (file_variable, argument_list);
write (file_variable, argument_list);

Similarly with readln and writeln. file_variable is declared as follows:

  filein, fileout : text;

The text data type indicates that the file is just plain text.

After declaring a variable for the file, and before reading from or writing to it, we need to associate the variable with the filename on the disk and open the file. This can be done in one of two ways. Typically:

reset (file_variable, 'filename.extension');
     rewrite (file_variable, 'filename.extension');
reset opens a file for reading, and rewrite opens a file for writing. A file opened with reset can only be used with read and readln. A file opened with rewrite can only be used with write and writeln.

Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call reset or rewrite using only the variable.

assign (file_variable, 'filename.extension');
reset (file_variable)

The method of representing the path differs depending on your operating system. Windows uses backslashes and drive letters due to its DOS heritage (e.g. c:\directory\name.pas), while MacOS X and Linux use forward slashes due to their UNIX heritage.

After you're done with the file, you can close it with:

close (File_Identifier);

Here's an example of a program that uses files. This program was written for Turbo Pascal and DOS, and will create file2.txt with the first character from file1.txt:

program CopyOneByteFile;

   mychar : char;
   filein, fileout : text;

   assign (filein, 'c:\file1.txt');
   reset (filein);
   assign (fileout, 'c:\file2.txt');
   rewrite (fileout);
   read (filein, mychar);
   write (fileout, mychar);

back to top


EOLN is a Boolean function that is TRUE when you have reached the end of a line in an open input file.

eoln (file_variable)

If you want to test to see if the standard input (the keyboard) is at an end-of-line, simply issue eoln without any parameters. This is similar to the way in which read and write use the console (keyboard and screen) if called without a file parameter.


EOF is a Boolean function that is TRUE when you have reached the end of the file.

eof (file_variable)

Usually, you don't type the end-of-file character from the keyboard. On DOS/Windows machines, the character is Control-Z. On UNIX/Linux machines, the character is Control-D.

back to top


You can compile your work by pressing "F9" when in the IDE. Or if you wanna launch it directly after compiling, just press "Ctrl+F9"!!

..:: by_Zain Fathoni ::..
.: SMA N 1 Jember / XII.Rintisan :.
copyright © 2008
Adapted from www.tayoue.com