Jump to content
Toggle sidebar
JookWiki
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Navigation
Main page
Recent changes
Random page
All pages
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information
Editing
Recursion
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
More
Read
Edit
Edit source
View history
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!
==Loops== Recursion can create loops without language constructs. Here's an infinite loop: function infinite_loop() print("Hello there!") return infinite_loop() end infinite_loop() This is a bit longer than a non-recursive example. Here's a counting loop: function count_down(number) if number == 0 then return end print(number) return count_down(number - 1) end count_down(100) A non-recursive version of this would likely use some kind of for or while loop. Here's a loop that asks a user to pick a valid choice: function get_choice(choices) local line = io.read() choice = choices[line] if choice then return choice else print("Invalid choice! Try again") return get_choice(choices) end end print("Select a letter to get a number: A, B, C") choice = get_choice({A=1, B=2, C=3}) print("You picked number " .. choice) Without recursion this code would look a lot more confusing, at least to me.
Summary:
Please note that all contributions to JookWiki are considered to be released under the Creative Commons Zero (Public Domain) (see
JookWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, 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.
Do not submit copyrighted work without permission!
To edit this page, please answer the question that appears below (
more info
):
Who owns this wiki?
Cancel
Editing help
(opens in new window)