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

I cant see the intended meaning of calling for loops, for loops?

Asked by 4 years ago

Recently I tried to experiment with for loops, I think that I do understand how they work, however, I cant see the intended meaning of calling for loops, for loops.

0
For loops get their name from programming logic stating that you repeat something for a given number of times. This might seem like the equivalent to a repeat loop, but a repeat loop keeps executing until a given condition is met. DeceptiveCaster 3761 — 4y

6 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

This is the 6th answer, but one that I hope answers your question from my point of view. I assume that by "can't understand the intended meaning" you mean that you know how the loops work, but don't understand why they're useful. There's actually a pretty simple answer.

In programming, 90% of the time you use a loop it will be to go through and do something with every value in an object or array. As I'm sure you already know, you can do this pretty easily with a while loop. Here's a piece of code that will loop through every value in an array and add one to it:

local myArray = {1,2,3,4,5}

local ctr = 1 --Keeps track of where we are in the array
while ctr <= #myArray do
    myArray[ctr] = myArray[ctr] + 1
    ctr = ctr+1
end

For loops and their variants are really just cleaner, easier, and prettier ways to do the exact same thing as above. For example, here's the above code rewritten using a for loop:

local myArray = {1,2,3,4,5}

for ctr = 1, #myArray do
    myArray[ctr] = myArray[ctr] +1
end

As you can see, the for loop does a few things for us: Defining our "ctr" variable inside it makes it clear that it is what is being increased by the loop, and the loop takes care of the actual iteration of the counter (The ctr = ctr+1 part isn't needed), as well as automatically stopping when we get to a certain value. As you can also see, the example with the for loop is also much easier to understand.

However, it doesn't stop there. There's an even cooler for variant called a for-each (or generic-for, depending on the language) that takes care of having to write all those ugly myArray[ctr] statements. Here's an example using those:

local myArray = {1,2,3,4,5}

for index, value in pairs(myArray) do
    value = value+1
end

Here, the loop takes care updating both your index, and value, allowing you to completely get rid of those ugly array accesses (myArray[ctr]). This style of loop is defiantly my most used, as it is very clear what you are trying to do inside it.

In conclusion, when looping for a fixed amount of times (such as through an array, selecting 100 random numbers, etc) it is beneficial to use a for loop because it's easier and cleaner.

Edit: It's called a "for" loop because you're doing something **for **every value in a given range (Or atleast that's my impression)

Ad
Log in to vote
3
Answered by
SmartNode 383 Moderation Voter
4 years ago

Loops are useful to recourse functions until a something is completed, or like a loading screen that constantly does

Loading.

Loading..

Loading...

(Looped) until the game has been fully loaded. Trust me when your at a decent level of a developer loops are your best friend.

1
Oh thanks but I am asking for "for loops" specifically why are they called "for" loops. Its the "for" that i cant understand. AndriusTheGreat 140 — 4y
0
That’s just an identifier translated so that it’s easier to identify in English. For, follows the next arguments which serves the instructions of how it’s going to work. Like for i = 1, 9 do which follows i, starting from one going up to 9 which has an increment if 1 by default. SmartNode 383 — 4y
Log in to vote
1
Answered by 4 years ago

I saw your other comment and basically it's just "do X for a range (Y)" since when you create a for loop you do something like this

for i = 1,10 do
    print("test")
end

You define a range through 1 to 10 and therefor repeat the function 10 times since there are 10 elements to the range.

Log in to vote
1
Answered by 4 years ago

For loops does a snippet of code for each item in a set of things. Hope this answers your question.

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

(note that what I write below is why I think it is called that, which may not be the actual reason.)

When using a generic for loop, like

for key, value in pairs(table) do
    print(key, value)
end

it means that for each key/value pair in table go and print the key and value.

When using a numeric for loop, like

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

it means that, for each value starting at 1 and ending with 10 which is divisible by 2, go and print the value.

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
4 years ago
Edited 4 years ago

A for loop is a loop that loops a certain amount of times, unlike the while loop which will loop forever.

for i = x, y, z do
    print(i)
end

This our for loop, now here is what is going on. x is the number that we are going to start from, y is the goal number, our end, and z which goes by the name of Increment is by how much we are going up, how much we are adding, each time we loop.

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

If you were to do this and print i which is the index variable, and pretty much the number that we are currently on each time it loops, you can see it prints 0 2 4 6 8 10. Because really what we are saying here is, we wanna start from 0 to 10 and go up by 2 each time, so the first time it loops it will be 0, the second time it loops we are 2 because we went up by 2, then 4, 6 and so on. So yeah, it's pretty cool if you think about it.

Let's see another example.

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

This will go from 5 to 25 with adding 1 each time, so we will go 5, 6, 7 .... 24, 25.

And that's really just for loops, I hope you understood.

Another thing we can point out is that, since I said a for loop loops a certain amount of times unlike a while loop that loops forever. And since it loops infinitly anything that's beyond , after, the while loop will not run.

print("hello")
for i = 0, 10, 1 do
print("hi")
end
-- after the loop finishes looping the amount of times its supposed to
--anything thats after it will run.
print("bye")

So in the script above, we print hello, then get into the for loop and printing hi 10 times, becaue the loop looped 10 times (since we are going from 0 to 10 by adding 1, so yeah technaclly 10) then after we are done looping we get out of the for loop and now all code behind the for loop runs.

Unlike a while loop we get stuck there, looping forever and anything after the while loops wouldn't run (until the while loop is done somehow)

print("hello")
while true do
print("hi")
end
--anything here will never run

You can even utilise this to make some cool stuff. like let's say, what if we wanna make a part gradually and slowly dissapear, by slowly changing the transparency property, since the transparency property is a number that goes from 0 to 1, we will go from 0 to 1 with an incrememnt of 0.1 maybe. That should be easy to understand. And also we will add a wait because the for loop will loop through very quickly, actually it's instant, so if we add a wait it should wait a bit before each loop. And since i is the current number that we are looping, we set the transparency to i.

local part = game.Workspace.Baseplate

for i = 0, 1, 0.1 do
    wait(0.5)
    part.Transparency = i
end

But for the use of for loops, well that depends on what you wanna do, when people first made programming languages they didn't totally care about all the posibilities, they just made these things, and out of luck they managed to make everything possible. So yeah it depends on what you wanna do, you can use it to iterate through tables (this another topic that i'm not gonna cover if you don't know about it) or you can make gradually dissapearing or moving things like we just did here, and also for somehting called lerp.

Really the question to ask here isn't when to use for loops, it's when should I use for loops?. Its when you feel like you should use a for loop because you're out of other ideas.

And that's really it, but again, there is much much more to tackle, but these are the basics here is a good explanation on loops

Answer this question