Basic Pascal Tutorial/Chapter 2/Input: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 2A - Input (author: Tao Yue, state: changed) | |
| 2 | 2 | ||
| 3 | 3 | Input is what comes into the program. It can be from the keyboard, the mouse, a file on disk, a scanner, a joystick, etc. | |
| 4 | 4 | ||
| 5 | 5 | 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. | |
| 6 | 6 | ||
| 7 | 7 | The basic format for reading in data is: | |
| 8 | 8 | ||
| 9 | 9 | ``` pascal | |
| 10 | 10 | read (Variable_List); | |
| 11 | 11 | ``` | |
| 12 | 12 | ||
| 13 | 13 | `Variable_List` is a series of variable identifiers separated by commas. | |
| 14 | 14 | ||
| 15 | 15 | `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: | |
| 16 | 16 | ||
| 17 | 17 | ``` pascal | |
| 18 | 18 | readln (Variable_List); | |
| 19 | 19 | ``` | |
| 20 | 20 | ||
| 21 | 21 | Suppose you had this input from the user, and `a, b, c,` and `d` were all integers. | |
| 22 | 22 | ||
| 23 | - | ||
| 24 | - | ||
| 23 | + | ``` | |
| 24 | + | 45 64 97 | |
| 25 | + | 1 2 3 | |
| 26 | + | ``` | |
| 25 | 27 | ||
| 26 | 28 | Here are some sample `read` and `readln` statements, along with the values read into the appropriate variables. | |
| 27 | 29 | ||
| 28 | 30 | | Statement(s) | a | b | c | d | | |
| 29 | 31 | |-------------------------------|-----|-----|-----|-----| | |
| 30 | 32 | | read (a); read (b); | 45 | 64 | | | | |
| 31 | 33 | | readln (a); read (b); | 45 | 1 | | | | |
| 32 | 34 | | read (a, b, c, d); | 45 | 64 | 97 | 1 | | |
| 33 | 35 | | readln (a, b); readln (c, d); | 45 | 64 | 1 | 2 | | |
| 34 | 36 | ||
| 35 | 37 | 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). | |
| 36 | 38 | ||
| 37 | - | ||
| 39 | + | ``` | |
| 40 | + | 8352.38 | |
| 41 | + | ``` | |
| 38 | 42 | ||
| 39 | 43 | 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. | |
| 40 | 44 | ||
| 41 | 45 | 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. | |
| 42 | 46 | ||
| 43 | 47 | 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. | |
| 44 | 48 | ||
| 45 | 49 | Make sure that all identifiers in the argument list refer to variables! Constants cannot be assigned a value, and neither can literal values. | |
| 46 | 50 | ||
| 47 | 51 | --- | |
| 48 | 52 | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 2/Input", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
