Home > General > Input and Output

Input and Output

Input from the keyboard

{input}
Readln(Number1);

When the above statement is executed the program will pause for a while and wait for the user to type something in from the keyboard, which will appear on screen, and then press enter. What they have typed would be, in the example above, stored in the variable Number1.

Output to the console
However we do also want to display text on the screen, to communicate with the user. So therefore we use the following output statement:

{output}
Writeln(Number1);

Below would be a simple program which uses both these principles.

program InputOutputProgram;

{$APPTYPE CONSOLE}

uses SysUtils;

var
	UserName : string[30];
begin
	Writeln( 'Hello! ');
	Readln;
	Write('Please enter your name: ');
	Readln(UserName);
	Write( 'Your name is: ' );
	Writeln( UserName );
end.

Run the above program, what is the effect of using “Write” and “Readln” on its own? “Writeln” moves the output stream to the next line whilst “Write” keeps the output stream on the current line. Using “Readln” on its own is used to wait for the user to press enter before continuing to the next instruction.

Categories: General Tags:
  1. No comments yet.
  1. No trackbacks yet.