| «‹ March 2015 ›» | | S | M | T | W | T | F | S | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | 15 | 16 | 17 | 18 | 19 | 20 | 21 | | 22 | 23 | 24 | 25 | 26 | 27 | 28 | | 29 | 30 | 31 | |
|
2009 年 9 月 11 日 星期五  |
| I/O Statement |
分類: Programming Language |
High-level Programming Language (Pascal)
Program Input and Output
To be useful, a program needs some means to communicate with the user. They are input statements and output statements.
Input Statements
- are used to enable the user to input data
- to get raw data prepared by the user in order to start processing
- to get responses from the user when there are more than one action which the program can take
- it is a good practice to include some kinds of prompt whenever input is required
- there are two input statements in Pascal : read and readln (read as read line)
- read and readln are predefined input procedures
- can read different types of data values from the keyboard or data file and assign the data to the corresponding variable(s)
- the data type to be read must be consistent with the variable(s) declared in variable declaration part (the only exception is that an integer can be assigned to a real-type variable, because an integer is a subset of real numbers)
- general forms of the input statements
read (<input list>)
readln (<input list>)
readln
- where <input list> is a single variable or a list of variables separated by commas
- only one string can be included in the input list of read or readln and it must be the last in the list
- during data input, data values must be separated by one or more space(s)
- execution of these input statements reads values from the standard file input and assigns them to the variables in the <input list>
- whenever executes a readln statement it pauses and waits for the required data
- after values have been read for all the variables, readln causes an advance to a new input line from which subsequent values will be read
- whereas read causes no such advance
- the last form of readln with no <input list> terminates input from the current line, so that subsequent input begins on a new line (i.e. program execution is suspended, providing an opportunity for the user to examine the output)
- type of argument of the input statements
|
Type of Argument
|
Action of read on the argument
|
|
char
|
Assign the next input character to its argument |
|
integer
|
Skip any blank, tab or carriage return, then read an integer that must end with a blank, tab or carriage return. The ending character is not read. |
|
real
|
Same as integer but a real number is read. |
|
string
|
Read all characters up to, but not including the carriage return. |
Output Statements
- provides a method for easily displaying information
- to output messages or results of a program to the user
- there are two output statements in Pascal : write and writeln (read as write line)
- write and writeln are predefined output procedures
- general forms of the output statements
write (<output list>)
writeln (<output list>)
writeln
- where <output list> is a single expression or a list of expressions separated by commas and each of these expressions may be a constant, a variable or a formula
- writeln statement will display program results and move the cursor to the beginning of next line
- write statements will display program results without moving the cursor to the beginning of next line
- the last form of writeln with no output list will cause the cursor to advance to a new line to display a blank line
- a list of examples
|
Action
|
Statement
|
Output
|
| 12345678901234567 |
Printing a number
Positive integer
Negative integer
Real number |
writeln (123);
writeln (-123);
writeln (123.0);
|
123
-123
1.230000000E+02 |
| Printing a character |
writeln ('A'); |
A |
| Printing a string |
writeln ('Hello'); |
Hello |
| Printing a list of data items |
writeln ('A', 'B', 'C');
writeln ('Hello', 'John');
writeln (1, '+', 2, 'is ', 3); |
ABC
HelloJohn
1+2 is 3 |
| Printing an expression |
writeln ('Sum = ', 2 + 6); |
Sum = 8 |
| Printing Boolean |
writeln ('3>2 is ', 3>2); |
3>2 is TRUE |
- The difference between write and writeln
|
Statements
|
Output
|
| 123456789012345678901234567 |
writeln ('Hello');
writeln ('Welcome to Pascal Programming'); |
Hello
Welcome to Pascal Programming |
write ('Hello');
write ('Welcome to Pascal Programming');
|
HelloWelcome to Pascal Programming |
|
|