read\readln
write\writeln
Formatting output

In a program, you don't always handle values that are known before. Sometimes you need the feedback of those who should use your program, sometimes your program needs values which are not always the same. For this, you should have a means of entering input to a program. For this, Pascal has the predefined read and readln functions.

read\readln

If you need an input from the keyboard or any other device, do not forget to prompt the user for input, otherwise he will not know when to input the data. Say, you need the age of the user. What you should do is as follows: Prompt the user to input data in a formal way, take in the input of the user. How to do it in Pascal: 
program ageofuser(input,output):
var
age:integer;
begin
writeln('Please type in your age '); {Here you prompt for input of the user}
readln(age); {Store the input into the variable 'age'}
end.
Here, as soon as the program runs, you see the sentence 'Please type in your age' on the screen, waiting for your input. Then you can enter a value for the age and press return after that. The program waits until you press the carriage return. If you have supplied an illegal age, that is an alphabetic symbol e.g., then you get a runtime error. To avoid this stick to the format of the required input.

Now, you wonder what the difference is between read and readln. The difference is that, if you have a whole line of symbols as input and read is used, than the cursor is forwarded to the next data item in the line, that is to the next character if the inputs should be characters, or the next number, if the inputs should be numbers. If you had used readln, the cursor would not be forwarded to the next data item, but to the beginning of the next line. Let's take an example:

Suppose you have 2 lines of inputs:
 
1 2 3 45 54 7 8 9 
100 54 87 56

Let the input statements be read(a); read(b); one after the other and a and b being integer variables. Then a gets the value 1, then the cursor goes one step to the right and b gets the value 2. If on the other hand, you had readln(a); read(b); then a would get the value 1 and b would get the value 100.

The applet below is for you to experience the difference between read and readln. In the leftmost box you can enter your input text, do not forget to press carriage return after each line. In the second box you can enter your sequence of input statements, that is a number of reads and readln's, do not forget the variables in the parentheses. In the rightmost box, you will see the values of the variables displayed. Be aware that this is not the output(!) you would see on the screen, just the values of the variables are shown there.

Another important feature of this applet is that it only handles character variables. The aim here is not to have a compiler online, but to show you the difference of read and readln. So if you have a space between your characters, it is possible that a variable has a space assigned as value and you don't see it displayed, because it is a space!

Here is a sample you could enter into the boxes: (do not forget to click the button after you have supplied all necessary data)
 
1. box:   2.box
123   readln(i); read(o);
456   read(d);

Illustrative Example

write\writeln

You may need to, most possibly, write anything onto the screen. It may be the response to the input, a prompt for input, an information, anything. For this, Pascal has the predefined functions write and writeln.

The syntax is write(outputdata) or writeln(outputdata), where outputdata is any variable or a text you specify. If it is text, you have to give it in quotes, that is like write('I'm a student'). Do not forget the semicolon after each line. The difference between write and writeln is that the cursor is forwarded to the end of the text if using write, but in case of writeln, the cursor is forwarded to the beginning of the next line.See the difference in the next examples.
 
program writing(input,output);
begin
write('text1');
write('text2'):
end.
Here the output would be :
text1text2.
program writing(input,output);
begin
writeln('text1');
write('text2'):
end.
Here the output would be :
text1
text2

Formatting output

With write and writeln you can also specify the format of the output of a variable.

Integer

write(number : fieldwidth);

A field of width "fieldwidth" will be reserved and "number" will be right-justified in this field with the remaining places on the left filled with blank. If "fieldwidth" is not large enough to cover all digits of "number", it will be correctly displayed in a wider field. "fieldwidth" may also be a variable or expression of type integer.

Real

write(number : fieldwidth : precision);

"fieldwidth" is not different from the one explained in the format of an integer. However, precision is something new. Precision defines the number of digits after the decimal point. If fieldwidth is not defined, scientific notation is used and the number of digits after the decimal point depends on fieldwidth. If both precision and fieldwidth are not specified, a default will be used. By scientific notation it is meant that 120 is shown as 1.2E+02 on the screen, that is 1.2*10 to the power 2.

Boolean

write(flag : fieldwidth);

This will output "true" or "false" within the specified, if specified, fieldwidth.

Char

write(ch : fieldwidth);

The character is output within the specified, if specified, fieldwidth. 

A First Look A First Look at Pascal Basic Input/Output Identifiers & Types
Constants Standard Functions If..Then Statement Case Statement
Repetitions Procedures and Functions Enumerated /Subrange Types Sets
I/O, Text Files Arrays Records Recursion
Pointer How to Debug Previous Exam Questions Advanced
ASCII Code Table Homepage University Homepage Computer Engineering Dept.