[banner image]
taoyue.com : Learn Pascal tutorial : 2A - Input

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

If we executed one of the following statements, this is what would be stored in the appropriate variables.

Statement(s) a b c d
read (a);
read (b);
4597
readln (a);
read (b);
451
read (a, b, c, d);45 9731
readln (a, b);
readln (c, d);
459712

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).

8352.38

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, there is one 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.

<<< Previous Table of Contents Next >>>

taoyue@mit.edu
Copyright © 1997-2004 Tao Yue. All rights reserved.