Input is one of the most important things in programming. This time you'll learn how the
keyboard input process works with the read and readln commands in Pascal.
The input command is tightly tied to the use of variables, which later go through data operations. Input lets you fill a variable while the program runs, so you no longer have to edit the code just to change a value.
Using the readln Command
In the Pascal programs we've built so far, you've surely met the readln command. It's
short for read line, so its job is to read input typed on one line.
To picture how the input process works, try typing this Pascal code.
program input;
uses crt;
var
nama: string;
umur: integer;
begin
clrscr;
write('nama: ');
readln(nama); // name input
write('umur: ');
readln(umur); // age input
// print name and age
writeln; // add a blank line
writeln('nama saya ', nama);
writeln('saya berusia ', umur, ' tahun');
readln;
end.
Once you run it, the program stops at "nama: ". There you type your name and press enter. Next it prints "umur: ", so type your age and press enter.
If I enter "Budi" and "17", the output looks roughly like this.
nama: Budi
umur: 17
nama saya Budi
saya berusia 17 tahun
From that output you can see that the variables nama and umur hold exactly what you typed. The variable nama holds "Budi", and umur holds "17".
The input process also has to respect the data type. The variable nama is a string, so it only takes a word or a sentence. The variable umur is an integer, so it only takes whole numbers.
A string variable can accept digits as input, but its value can never go through a calculation. The digits you typed are still read as words.
For an experiment, try changing the name and the age you enter. And try entering an age with a decimal, say 9.2.
Using the read Command
The read command works like readln, since it's an input system too. The only
difference is how the data gets into the variable.
With readln you have to press enter before the input lands in the variable. With
read you can use the space bar or enter to put the input into the variable.
Now let's modify the code we wrote a moment ago.
program input;
uses crt;
var
nama: string;
umur: integer;
jmlsaudara: integer; // add a new variable
begin
clrscr;
write('nama: ');
readln(nama); // name input
writeln('masukkan umur dan jumlah saudara dipisahkan spasi');
read(umur); // age input
read(jmlsaudara); // siblings input
// print name and age
writeln; // add a blank line
writeln('nama saya ', nama);
writeln('saya berusia ', umur, ' tahun');
writeln('saya punya ', jmlsaudara, ' saudara')
readln;
end.
Run that code, type a name, and press enter. Then enter the age and the number of siblings separated by a space, for example "29 4", and press enter. The output should look something like this.
nama: Adi
masukkan umur dan jumlah saudara dipisahkan spasi
29 4
nama saya Adi
saya berusia 29 tahun
saya punya 4 saudara
You can see it matches. The variable nama holds "Adi", while umur and jmlsaudara hold "29" and "4".
If you want to use
readfor a string variable, you have to separate the values with the enter key. A space typed into a string is also read as a character.
As usual, as an experiment you can try changing the input you type.
Closing
So Pascal has 2 commands for input: read and readln. What separates them is only how
the value gets into the variable. readln needs enter, while read accepts a
space.
Hope this tutorial helps :D In the next one we'll try building a simple Pascal program, combining everything we've learned so far.