Code patternsInput and outputEcho until the end of the input
Echo until the end of the input
Prints everything it reads and stops when the input ends, on any interpreter.
Watch it run
The form “For all three conventions” works on 3 cells; the named ones are “zero” and “letter”. Press Play, drag the step slider forwards and back, or edit the input:
The figure needs JavaScript. The program below is the result for the example values.
The loop itself is simple: read, and while there is a letter, print it and read again. The hard part is "while there is a letter".
Brainfuck never fixed what , does when the input is over. Some interpreters put 0 in the cell, some put −1, some leave the cell as it was. A program that only knows one of the three hangs on the others.
The portable form covers all three. It clears the cell before each read, so "left as it was" becomes 0. And it tests each letter twice: is it 0, and is it 0 after adding 1 (which catches −1).
The code
This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
>,[+[-.[-],>]<]
What it needs
- Portable form: one zero cell on each side of the working cell. It starts on cell 0 and reads into cell 1.
- The pointer ends on cell 1 when the end was 0 or "no change", and on cell 0 when it was −1.
- A byte with all bits set (255) in the input ends the portable form early.
Common mistakes
- Testing a program with one interpreter only. Use the choice "At the end of the input, the comma gives", in More controls under the first figure, to try all three answers.
- Using
,[.,]where the end of the input is −1: it prints the byte 255 for ever.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| For all three conventions | grows with the letter codes | 2 + 8 per letter |
| End of input is 0 | 2 + 3 per letter | 2 + 3 per letter |
| End of input is −1 | 3 + 5 per letter | 3 + 5 per letter |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| For all three conventions | the input = "" | 8 | 15 | 3 | 2 |
| For all three conventions | the input = "abc" | 8 | 15 | 621 | 26 |
| End of input is 0 | the input = "abc" | 8 | 5 | 11 | 11 |
| End of input is −1 | the input = "abc" | 8 | 8 | 18 | 18 |
Other forms
End of input is 0
The shortest form. Also the most common convention.
,[.,]
Run this form step by step
The form “End of input is 0” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the input:
The figure needs JavaScript. The program below is the result for the example values.
End of input is −1
Adds 1 before the test and takes it off again inside.
,+[-.,+]
Run this form step by step
The form “End of input is −1” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the input:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- Every program that reads until the end: read a line into memory, reverse a string.
- Brainfuck basics, lesson 6, "Reading input", builds the short form in its section "Until there is nothing left".
- The reference page Where implementations differ shows the three answers in its section "The end of the input", and its compatibility table lists what six implementations and descriptions do.
Sources
- Source: Esolang wiki: Brainfuck algorithms: the −1 test is the one in "Input a decimal number" (Urban Müller); the arrangement here is our own.
- Source: Esolang wiki: Brainfuck, "EOF": the three conventions.
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.