Befunge Techniques

Jumping Over Cells with the Trampoline

What the Befunge # instruction does, how it skips exactly one cell in any direction, and the loop and layout tricks it makes possible.

On this page
  1. What the instruction does
  2. One cell, two uses
  3. Hopping over cells that belong to another route
  4. Skipping in string mode and at the edges
  5. When to avoid it
  6. Related ideas

What the instruction does

The hash character # is called the trampoline. When the pointer executes it, the pointer moves on as usual, but the next cell in its path is skipped entirely and not executed. Execution resumes on the cell after that.

Three details are worth fixing in your mind.

  • It skips exactly one cell, whatever that cell contains. An instruction, a digit, a space or a quote character are all treated the same way.
  • It works in every direction. Moving west, it skips the cell to its west. Moving down, it skips the cell below.
  • The skip is relative to the way the pointer is moving at that moment, so the same # can skip different cells depending on which side it is approached from.

A first example shows the idea:

1#2.@

The pointer pushes 1, reaches the #, jumps over the 2, and executes the . next. The program prints 1 and stops. The 2 is never pushed. Written like this the trampoline is pointless, because you could just delete the 2, but in larger programs the skipped cell has a job when the pointer comes from the other direction.

One cell, two uses

Because a cell can be reached from both sides, it can do something different each time. The classic case is the print-until-zero loop.

0"!iH">:#,_@

Here 0"!iH" pushes a zero and then the codes for !, i and H, so H is on top. Then the pointer meets >, :, #, ,, _ and @.

Moving east, the pointer duplicates the top value, then the # skips the ,, and _ pops the copy. If the copy is nonzero, _ sends the pointer west. Now the pointer reaches the , from its other side and executes it, printing and removing the original value. Next to the west is the # again, now approached from the east, so it skips the cell to its west, which is the :. The pointer lands on the > and heads east once more.

So on the way east the , is skipped and on the way west the : is skipped. Each pass prints one character, and when the top value is the zero the duplicate tests as zero, _ goes east, and the @ ends the program. The output is Hi!. The same cells form both the loop body and the test, which is why this arrangement turns up so often in short programs and in code golf.

Hopping over cells that belong to another route

The trampoline also lets two paths share space. Suppose a vertical route passes through a column, and a horizontal route must cross it. Both routes would normally execute whatever occupies the crossing cell. If that cell holds an instruction that only the vertical route wants, the horizontal route can place a # just before it and step over.

A related use is skipping a turning arrow. In the countdown example, the pointer passes east over a v by skipping it and later arrives at the v from the east so that it turns downward. One physical cell is thus inert in one direction and active in the other. This saves an entire row or column compared with giving each direction its own cells.

Skipping in string mode and at the edges

Two special cases need care.

First, the skipped cell is not examined at all. If you skip over a double quote, string mode does not toggle, which is sometimes exactly what you want and sometimes a bug. Equally, if the skipped cell is the last character of a string mode region, it will not close the string. When a program behaves strangely around quotes, look for a stray # nearby.

Second, the skip can cross the edge. Since the playfield wraps around, a # at the end of a row skips the first cell on the opposite side. This is a consequence of the toroidal playfield rather than a special rule, but it surprises people who expect the jump to stop at the boundary. In Befunge-93 the playfield size is fixed, and those Befunge-93 limits decide where the wrap happens, so it is worth testing a skip near the edge rather than assuming.

When to avoid it

The trampoline is easy to overuse. A program with many skips is hard to read, because a cell’s role depends on the direction the pointer approaches from, and the reader has to track that mentally. A few guidelines keep it manageable.

  • Use a trampoline when it removes a whole row of arrows, not to save a single cell.
  • Put it next to the cell it affects so the connection is obvious.
  • Remember that two in a row do not make a two-cell jump. A # directly followed by another # skips the second one. To skip more cells, use direction-changing arrows instead.
  • When a program looks like it loses a cell, check each # in the trace.

When debugging, list the cell the pointer lands on after each # rather than the cell it was aimed at. The two differ by exactly one, and that off-by-one is the main source of trouble.

For a gentler start, the guide on laying out Befunge code so humans can read it gives advice on keeping skips visible. For a program that exercises both the trampoline and a test cell, see the worked countdown program in the same cluster. And if you want a fuller picture of how instructions are interpreted one cell at a time, the program stack and instruction set pages are the companions to this one.

The complete guide to befunge techniques

Befunge Programming Techniques Overview
Read the guide

Keep exploring

Guides from other topics

All topics →

What Is Befunge?

A plain introduction to Befunge, the esoteric language where code lives on a two-dimensional grid and an instruction pointer moves in four directions.

Befunge Basics 5 min read