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 10 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
10 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:

for index,value in pairs(table) do

end

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:

local stuff = { "Apple", "Pear", "Orange" }

for i,v in pairs(stuff) do
    print(i)
    print(v)
end

--[[

Output:

1
Apple
2
Pear
3
Orange

]]
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 — 10y
1
Sure! Just added one :) MrFlimsy 345 — 10y
0
I have one more question, does this `for` loop require a `wait()`, or can it work without one? TheeDeathCaster 2368 — 10y
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 — 10y
0
Wow, thats awesome! Thank so much man, this helped allot! ;) TheeDeathCaster 2368 — 10y
Ad
Log in to vote
3
Answered by
TaslemGuy 211 Moderation Voter
10 years ago

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

While loops go like this:


while condition do body end

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:

repeat
    body
until condition

You can rewrite them as while-loops like this:

body
while condition do
    body
end

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:

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

This will print 1 2 3 4 5.

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

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

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:

function oneToTen()
    local i = 0
    return function()
        i = i + 1
        if i > 10 then
            return nil
        end
        return i
    end
end

So if we do:

for i in oneToTen() do
    print( i )
end

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

function squares()
    local i = 0
    return function()
        i = i + 1
        if i > 20 then
            return nil
        end
        return i^2
    end
end

And now we can do:

for n in squares() do
    print( n )
end

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 — 10y
Log in to vote
-4
Answered by
KAAK82 16
10 years ago

only while wait() do and math.huge

0
NOOBS! -_- KAAK82 16 — 10y
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 — 10y
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 — 10y
0
I feel ya man. ;-; Sometimes I get Thumbs'd Down when I post questions, even when there not requests. ;-; TheeDeathCaster 2368 — 10y
View all comments (2 more)
0
Yeah I know man, so annoying KAAK82 16 — 10y
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 — 7y

Answer this question