Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Befunge
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Sample Befunge-93 code == The technique of using arrows to change control flow is demonstrated in the random number generator program below. The Befunge instruction pointer begins in the upper left corner and will travel to the right if not redirected. Following the arrows around, the <code>?</code> instructions send the instruction pointer in random cardinal directions until the pointer hits a digit, pushing it to the stack. Then the arrows navigate to the <code>.</code> to output the digit from the stack and return the pointer to the first directional randomiser. There is no <code>@</code> to terminate this program, so it produces an endless stream of random numbers from 1 to 9. <syntaxhighlight lang="befunge"> v>>>>>v 12345 ^?^ > ? ?^ v?v 6789 >>>> v ^ .< </syntaxhighlight> The following code is an example of the classic [[Hello world program|"Hello World!" program]]. First the letters "olleH" are pushed onto the stack as [[ASCII]] numbers. These are then popped from the stack in [[LIFO (computing)|LIFO]] order and output as text characters to give "Hello". A space is character number 32 in ASCII, which here is constructed by multiplying 4 and 8, before being output as text. The remaining code then outputs "World!" in a similar way, followed by ASCII character 10 (a [[line feed]] character, moving the output cursor to a new line). <syntaxhighlight lang="befunge"> > v v ,,,,,"Hello"< >48*, v v,,,,,,"World!"< >25*,@ </syntaxhighlight> The following code is a slightly more complicated version. It adds the ASCII character 10 (a [[line feed]] character) to the stack, and then pushes "!dlrow ,olleH" to the stack. Again, [[LIFO (computing)|LIFO]] ordering means that "H" is now the top of the stack and will be the first printed, "e" is second, and so on. To print the characters, the program enters a [[Control flow#Loops|loop]] that first duplicates the top value on the stack (so now the stack would look like "[[line feed|\n]]!dlrow ,olleHH"). Then the "_" operation will pop the duplicated value, and go right if it's a zero, left otherwise. (This assumes a compliant interpreter that "returns" 0 when popping an empty stack.) When it goes left, it pops and prints the top value as an [[ASCII]] character. It then duplicates the next character and loops back to the "_" test, continuing to print the rest of the stack until it is empty and so the next value popped is 0, at which point "@" ends the program. <syntaxhighlight lang="befunge"> >25*"!dlrow ,olleH":v v:,_@ > ^ </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Befunge
(section)
Add topic