Input/Output (I/O) operations are essential for any useful program, allowing it to communicate with the user via the console (text window).
1. Basic Output: Write and Writeln
Pascal uses Write and Writeln to display text, variables, and calculation results on the screen.
| Command | Purpose |
Write(...) | Displays the output and keeps the cursor on the same line, ready for the next output immediately following the first. |
Writeln(...) | Displays the output and then moves the cursor to the next line (equivalent to pressing Enter). |
A. Displaying Simple Text
Text strings must be enclosed in single quotes (‘ ‘).
begin
Write('Hello'); // Output: Hello
Write(' World'); // Output: Hello World
Writeln('!'); // Output: ! (Cursor moves to next line)
Writeln('This is a new line.');
end.
B. Displaying Variables and Calculations
You can pass multiple items to Write or Writeln by separating them with commas.
program OutputDemo;
var
Name: String;
Age: Integer;
begin
Name := 'Max';
Age := 25;
// Output variables and text together
Writeln('Hello, ', Name, '. You are ', Age, ' years old.');
// Output a calculation result
Writeln('In five years, you will be ', Age + 5, ' years old.');
end.
2. Formatted Output (Writeln)
For numeric types, especially Real numbers, you can specify how much space the value should occupy on the screen using formatting directives. This is essential for aligning tables and controlling decimal places.
The syntax for formatting is: Variable : TotalWidth : DecimalPlaces
| Format | Purpose | Example | Output |
:TotalWidth | Minimum space the value occupies (right-aligned). Used for Integers and Strings. | Writeln(123:5); | 123 (2 spaces followed by 123) |
:TotalWidth :DecimalPlaces | Minimum space and the number of digits after the decimal point. Used for Real numbers only. | Writeln(3.14159:1:2); | 3.14 |
var
Pi: Real;
Count: Integer;
begin
Pi := 3.1415926;
Count := 42;
// 1. Integer formatting (TotalWidth: 8)
Writeln('Count: ', Count:8); // Output: Count: 42
// 2. Real formatting (TotalWidth: 10, DecimalPlaces: 3)
Writeln('Pi Value: ', Pi:10:3); // Output: Pi Value: 3.142
// 3. Real formatting (DecimalPlaces: 0 - truncates decimal)
Writeln('Rounded Pi: ', Pi:1:0); // Output: 3
end.
3. Basic Input: Read and Readln
The Read and Readln commands wait for the user to type something on the keyboard and then store that input into one or more specified variables.
| Command | Purpose |
Read(...) | Reads data from the keyboard into the variable(s) until a space or a return key is pressed. The cursor stays on the same line after reading. |
Readln(...) | Reads data from the keyboard into the variable(s) and then discards the rest of the current input line, moving the cursor to the next line. Readln is generally preferred for simple input. |
Example: Reading User Input
program InputDemo;
var
YourName: String;
YourFavoriteNumber: Integer;
begin
// 1. Prompt the user
Write('Enter your name: ');
Readln(YourName); // Read the string input
// 2. Prompt for a number
Write('Enter your favorite whole number: ');
Readln(YourFavoriteNumber); // Read the integer input
// 3. Output the result
Writeln; // Prints a blank line
Writeln('Hello, ', YourName, '! Your number is ', YourFavoriteNumber);
end.
Note on
Readlnwithout arguments: If you callReadln;(with no variables), the program simply pauses and waits for the user to press the Enter key. This is commonly used at the end of console programs to keep the output window open until the user is ready to close it (as seen in P1.2).
4. Character and String Functions
Pascal includes built-in functions for manipulating strings and characters:
| Function/Procedure | Purpose | Example |
Length(S) | Returns the number of characters in the string S (Integer). | Length('Pascal') $\rightarrow$ 6 |
UpCase(C) | Converts a character C to its uppercase equivalent. | UpCase('a') $\rightarrow$ 'A' |
Pos(Sub, S) | Returns the starting index of the substring Sub within the string S. Returns 0 if not found. | Pos('al', 'Pascal') $\rightarrow$ 5 |
Delete(S, Index, Count) | Removes Count characters from string S starting at Index. | Delete(S, 1, 1) (Removes first char) |
Insert(Sub, S, Index) | Inserts substring Sub into string S at Index. |
