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

should I put a break under or over an if statement inside a for loop?

Asked by 4 years ago
local children = script.Parent.Parent:GetChildren()
for i = 1, #children do
    if children[i]:IsA("BasePart") then

-----should a break go here-------
        print("script ran")
-----or here?---------

    end
end

I want the loop to break when children[i] is a basepart.

for the code under the for loop and the if statement, were is the best place to put a break.

1 answer

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

Now that I understand the question, break goes after any code you want to run. Code executes top to bottom, so generally put break after any code you want to run.

local children = script.Parent.Parent:GetChildren()
for i = 1, #children do
    if children[i]:IsA("BasePart") then
        print("script ran")
        break --here
    end
end

If you put break before the print, it would exit the loop first, so nothing would be printed.

Ad

Answer this question