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:

>,[+[-.[-],>]<]
Open in the playground15 characters

What it needs

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
For all three conventionsgrows with the letter codes2 + 8 per letter
End of input is 02 + 3 per letter2 + 3 per letter
End of input is −13 + 5 per letter3 + 5 per letter
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
For all three conventionsthe input = ""81532
For all three conventionsthe input = "abc"81562126
End of input is 0the input = "abc"851111
End of input is −1the input = "abc"881818

Other forms

End of input is 0

The shortest form. Also the most common convention.

,[.,]
Open in the playground5 characters
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.

,+[-.,+]
Open in the playground8 characters
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

Sources

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.