ZetCode

Algol 68 reading input

last modified November 13, 2026

A program reads from stand in, the predeclared file for the standard input, with the same procedures that write to stand out. read converts the text into values of the requested modes, while get takes the text as it comes. This article describes both, the prompts that a program prints, the handlers for bad input and for the end of the input, and the loops that read until a value marks the end.

The transcripts below show what the program writes. The lines that the reader types are not part of the output, so every example prints the values it has read to make the result visible. Each prompt is written with newline, so the program starts reading on a fresh line.

Reading an integer

read takes a row of values and reads them from the standard input, while get takes a file first and then the row. The mode of each value decides how the text is converted, and the newline at the end of the row discards the rest of the line, so the next read starts at the beginning of the following line.

readint.a68
BEGIN
  INT n;
  print(("Enter a number:", newline));
  read((n, newline));
  print(("you entered ", whole(n, 0), newline));
  print(("twice that is ", whole(2 * n, 0), newline))
END

The declaration gives the variable the mode INT, and the call fills it with the number found on the line. The program then echoes the value and computes twice the number, which shows that n is an ordinary integer after the read. If the line does not begin with a number, the interpreter raises a runtime error, which the last sections of the article deal with.

$ a68g readint.a68
Enter a number:
you entered 21
twice that is 42

Reading a real

A REAL variable reads a number with a decimal point or an exponent. The conversion follows the same rules as a real denotation, so the number may be written as 2.5 or as 2.5e0.

readreal.a68
BEGIN
  REAL r;
  print(("Enter a real number:", newline));
  read((r, newline));
  print(("you entered ", fixed(r, 0, 2), newline));
  print(("half of it is ", fixed(r / 2, 0, 2), newline))
END

The value 2.5 is echoed with two decimals, and the division by two gives 1.25. An integer written at the prompt is accepted as well, because the conversion of a real accepts a digit sequence without a point.

$ a68g readreal.a68
Enter a real number:
you entered 2.50
half of it is 1.25

Several values on one line

The row of a read may hold several values. They are taken from the same line in order, separated by spaces, and the trailing newline again consumes what remains of the line.

readpair.a68
BEGIN
  INT a, b;
  print(("Enter two numbers on one line:", newline));
  read((a, b, newline));
  print((whole(a, 0), " and ", whole(b, 0), newline));
  print(("their sum is ", whole(a + b, 0), newline))
END

Two integers are declared in one declaration and filled by one call. The values are read from the line 3 4, and the program prints them and their sum. A third value on the same line would be ignored, because the newline closes the line after the second number.

$ a68g readpair.a68
Enter two numbers on one line:
3 and 4
their sum is 7

Reading a whole line

get(stand in, (line, newline)) reads the whole line into a STRING, spaces and all. This is the way to read text that is not a single number, such as a name or a sentence.

readline.a68
BEGIN
  STRING line;
  print(("Enter a line of text:", newline));
  get(stand in, (line, newline));
  print(("you wrote: ", line, newline))
END

The string receives the characters of the line without the newline, which the second item consumes. read treats a STRING value in the same way, so either procedure reads a line; get is the general one and takes the file first, while read works on stand in.

$ a68g readline.a68
Enter a line of text:
you wrote: hello world

Reading a character

A CHAR value takes the first character of the line and then discards the rest, because the row also contains a newline. The mode decides how much of the text is converted, not the procedure.

readchar.a68
BEGIN
  CHAR c;
  print(("Enter a word:", newline));
  read((c, newline));
  print(("the first character is ", c, newline))
END

The word zebra fills the variable with z. A program that needs the other characters reads the line as a string and subscripts it, which the strings article describes.

$ a68g readchar.a68
Enter a word:
the first character is z

Reading until a sentinel

A loop reads values until one of them marks the end. The loop below stops at zero, so the sentinel is not counted and not added to the total.

sumloop.a68
BEGIN
  INT n, count := 0, total := 0;
  print(("Enter numbers, one per line, then 0:", newline));
  read((n, newline));
  WHILE n /= 0 DO
    count +:= 1;
    total +:= n;
    read((n, newline))
  OD;
  print((whole(count, 0), " numbers, total ", whole(total, 0), newline))
END

The first read primes the loop, and the read at the end of the body takes the next number. The condition tests the value before it is added, so the final zero ends the loop without being counted. The numbers 3, 4, and 5 give three values and a total of 12.

$ a68g sumloop.a68
Enter numbers, one per line, then 0:
3 numbers, total 12

Recovering from bad input

on value error installs a handler for a file. The handler is a procedure of mode PROC (REF FILE) BOOL, and returning TRUE tells the interpreter that the error has been dealt with, so the program continues instead of stopping.

onerror.a68
BEGIN
  INT n := 0;
  on value error(stand in, (REF FILE f) BOOL: ( print(("that is not a number, using 0", newline)); TRUE ));
  print(("Enter a number:", newline));
  read((n, newline));
  print(("n = ", whole(n, 0), newline))
END

When the line holds letters instead of a number, the handler prints a message and yields true, and the variable keeps the value it had before the call, which is why it is initialised. Without the handler the same input ends the program with the message error transputting INT value.

$ a68g onerror.a68
Enter a number:
that is not a number, using 0
n = 0

The end of the input

on logical file end installs a handler that runs when the file has no more characters. A program that must read an unknown number of lines sets a flag in the handler and tests the flag around each read.

endinput.a68
BEGIN
  STRING line;
  INT count := 0;
  BOOL done := FALSE;
  on logical file end(stand in, (REF FILE f) BOOL: done := TRUE);
  WHILE NOT done DO
    get(stand in, (line, newline));
    IF NOT done THEN count +:= 1 FI
  OD;
  print((whole(count, 0), " lines read", newline))
END

The loop keeps reading until the handler sets done. The read that meets the end of the file still returns, so the counting is guarded by the same flag; without the guard the last, empty line would be counted. The three lines of the example give a count of three.

$ a68g endinput.a68
3 lines read

Next steps

The unformatted transput article describes the output side of read and get, and the formatted transput article shows the formats that go with getf. The files article reads and writes files of a program's own with the same procedures, and command-line arguments takes input from the arguments of the script instead.

Source

Revised Report on the Algorithmic Language Algol 68

The Algol 68 Genie project

In this article we have read integers, reals, strings, and characters from the standard input, taken several values from one line, printed prompts, looped until a sentinel value, recovered from input that does not match the mode, and detected the end of the input with a handler. The files article continues with the same procedures on files that a program opens itself.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Algol 68 tutorials.