How to use continue?

I have 0 idea how to use it. Any support thanks

4 Likes

It skips the code inside a loop.

Here’s a difference between return and continue.

for i = 1, 10 do
if i == 8 then
return
end

print(i)
end

print('Hello world!')
--> 1 2 3 4 5 6 7
for i = 1, 10 do
if i == 8 then
continue
end

print(i)
end

print('Hello world!')
--> 1 2 3 4 5 6 7 9 10 Hello world!

9, 10 and Hello world! wouldn’t print with return because return stops the running script or function.
I’m pretty tired so maybe I’m incorrect?

13 Likes

Your first code stops at 7 not 8?

3 Likes

Oh yeah forgot my bad
char limit

2 Likes

The second example, usage of continue, is correct. However, the first example, usage of return, is incorrect, as it’d break the loop once at 8 - therefore printing out as:
1, 2, 3, 4, 5, 6, 7 (separately)

3 Likes

I still don’t understand what you mean by skip the code

2 Likes

It isn’t ignoring the code


for i = 1, 10 do
	if i == 8 then
		local Part = Instance.new("Part")
		Part.Parent = workspace
		
		continue
	end
	
	print(i)
	
end
1 Like

continue is used to skip a child, or an integer, in a loop. As of currently, you’re using it to skip 8.

if i == 8 then --if i is 8 then
   continue --don't run code underneath, move on to 9
end
local part = Instance.new("Part")
part.Parent = game.Workspace
5 Likes

I’m the worst explainer change my mind.
Basically it wouldn’t do the code which is outside of the if the statement.
In the example I provided 8 would not print in the output.

The code you sent seemed to work fine as it skipped printing 8 and inserted the brick once since it’s only checking for just 1 number.

2 Likes

So it ignores everything under the continue

1 Like

That’s correct - the code basically checks if the integer is 8, and if it is, it doesn’t run the code beneath (at your situation, it won’t create an Instance), and moves on to 9. See for yourself:

for i = 1, 10 do
    if i == 8 then
        continue
    end
    local part = Instance.new("Part")
    part.Name = i
    part.Parent = game.Workspace
end

So using your example it will ignore any other code that is under the continue statement until I is 8. And if i is 8 then it will continue running the code below? It should ignore the code once

The code that I provided checks if the integer is 8, and if it is, then it won’t run the code underneath - therefore, you’re correct. You should understand how continue works now.

Okay this might be my last question. Why would studio crash when I reaches 10?

local i = 1

while true do
	if i == 10 then
		continue
	end
	
	local Part = Instance.new("Part")
	Part.Name = i
	Part.Parent = workspace
	
	i = i + 1
	
	wait(1)
end

Im guessing continue is the same as repeat

It’d crash because continue skips the rest of the code underneath, including wait(1), which prevents crashing on while true do. Here’s the fixed script:

local i = 1
while true do wait(1) --moved wait(1) so it's not underneath continue
	i += 1 --moved it here so it keeps updating every second, not being affected by continue
	if i == 10 then
		continue
	end
	local Part = Instance.new("Part")
	Part.Name = i
	Part.Parent = workspace
end
7 Likes

continue is used in loops, to move onto to the next iteration of the loop. It can’t be used in if statements.

1 Like

I believe they were just examining the relevant code, not the full code.

1 Like