Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Is there any other loops other than `while true do end repeat until nil for i=1,1 do end`?

Asked by 11 years ago

If so, I've never heard of it before, if there isn't any, I am sorry for posting.

3 answers

Log in to vote
3
Answered by
MrFlimsy 345 Moderation Voter
11 years ago

Don't be sorry for posting, that's what this website is for!

The only thing you're missing the generic for loop. It looks like this:

1for index,value in pairs(table) do
2 
3end

The variables index and value can be replaced with anything. table should be a valid table.

The code will loop through once for every value in the given table. Each time it loops, the 'value' variable will be equal to the value it's currently passing over, and 'index' will be equal to the number of times it's already looped. In programming lingo, this is called an iterator.

Edit: Here's an example:

01local stuff = { "Apple", "Pear", "Orange" }
02 
03for i,v in pairs(stuff) do
04    print(i)
05    print(v)
06end
07 
08--[[
09 
10Output:
11 
121
13Apple
142
15Pear
163
17Orange
18 
19]]
0
Wow, Thanks man! I never knew that kind of `for` loop did a loop, thank you so much! And can you give me an example of one, please? :) +1 TheeDeathCaster 2368 — 11y
1
Sure! Just added one :) MrFlimsy 345 — 11y
0
I have one more question, does this `for` loop require a `wait()`, or can it work without one? TheeDeathCaster 2368 — 11y
1
It will work without one, because unlike 'while true do' or 'repeat wait until nil' loops, it doesn't repeat infinitely and won't crash your client. MrFlimsy 345 — 11y
0
Wow, thats awesome! Thank so much man, this helped allot! ;) TheeDeathCaster 2368 — 11y
Ad
Log in to vote
3
Answered by
TaslemGuy 211 Moderation Voter
11 years ago

Lua has three kinds of loops. while, repeat, and for.

While loops go like this:

1while condition do
2    body
3end

Basically, when you get to the loop, you look at the condition.

If it's falsey, skip to the end. If its truthy, run body and then start over, checking the condition again.

Repeat loops are a lot like while loops. They look like this:

1repeat
2    body
3until condition

You can rewrite them as while-loops like this:

1body
2while condition do
3    body
4end

Basically, they always run their inside once before checking the condition. Usually, it's better to use while-loops, but these are occasionally useful.

For loops are the most special. There's the kind you mentioned:

1for i = 1, 5 do
2    print( i )
3end

This will print 1 2 3 4 5.

But they can do more than just that! You can choose an increment size:

1for i = 0, 10, 2 do
2    print( i )
3end

This prints 0 2 4 6 8 10

For loops also allow the use of iterators. The most famous iterators are pairs and ipairs.

An iterator is a function that when called, produces some output. When you call it again, it produces a different output, and so on. For instance, this generator will produce the numbers 1 to 10:

01function oneToTen()
02    local i = 0
03    return function()
04        i = i + 1
05        if i > 10 then
06            return nil
07        end
08        return i
09    end
10end

So if we do:

1for i in oneToTen() do
2    print( i )
3end

Or if we want to print the first 20 squares, we can do this:

01function squares()
02    local i = 0
03    return function()
04        i = i + 1
05        if i > 20 then
06            return nil
07        end
08        return i^2
09    end
10end

And now we can do:

1for n in squares() do
2    print( n )
3end

Iterators are extremely powerful, but they can be tricky to write. ipairs and pairs are iterators which iterate over tables, for instance, and they're used all the time.

0
You put a lot of effort into this. +1 MrFlimsy 345 — 11y
Log in to vote
-4
Answered by
KAAK82 16
11 years ago

only while wait() do and math.huge

0
NOOBS! -_- KAAK82 16 — 11y
0
'I didn't thumbs you down', `while wait do end` is one type of loop, it isn't the only one, and math.huge is not a loop, it can he used in different kinds of coding, but it is not a loop. TheeDeathCaster 2368 — 11y
0
some people use it as a loop :/ tons of Advanced Scripters use it, look in the Forums, also, thnx for not Thumbing me Down, cos all these Noobs Thumbs Down for me just trying to help people... KAAK82 16 — 11y
0
I feel ya man. ;-; Sometimes I get Thumbs'd Down when I post questions, even when there not requests. ;-; TheeDeathCaster 2368 — 11y
View all comments (2 more)
0
Yeah I know man, so annoying KAAK82 16 — 11y
0
This doesn't correlate towards the question at hand; I asked if there were other kinds of loops that aren't that of the standard ones: ones that're unknown or hidden, such as the generic four as @MrFlimsy had touched on & explained. TheeDeathCaster 2368 — 8y

Answer this question