What does the "continue" and the "return" statements do?

Hey guys, I’m trying to learn advanced scripting to start doing more advanced commissions and earn a bit more. I can’t understand what the continue statement does, could you explain to me What does the “continue” and the “return” statements do?

3 Likes

continue skips an iteration in the loop for example

for i=1, 10 do
	if i ==3 then continue end -- if ==3 then skip this iteration
	print(i) -- will print from 1 to 10 but willnot print 3 because the iteration was skipped when it was 3
end

return will return a value back for example

function calculate2Numbers(n1, n2)
	return n1 + n2 -- will return the result of (n1 + n2)
end

print(calculate2Numbers(5,5)) -- will print 10

or

function Three()
	return 3 -- will return 3
end

local myNumber = Three() -- my number will be equal to 3 because the Three() function returned 3

print(myNumber)
4 Likes

In basic terms, continue skips an iteration in a loop, and return either returns a value, or ends the function early. Heres some examples:

Uses for return

local word = "Hello!"

function ChangeWord(TheWord)
	word = TheWord
	return word
end

ChangeWord("World")

print(word)

Here we make a function to change a string and return it, we change the word variable to the parameter in the brackets and it comes back as that value, another use is to stop the function early.

local FirstPart = false
local SecondPart = false

function Run()
	print("The function is activated")
	
	if FirstPart == true then
		return
	end
	
	if SecondPart == true then
		return
	end
	
	print("The function is completed")
end

Run()

This will only print the last part, IF both of the conditions are true, otherwise it won’t do anything.

But continue skips an iteration in a loop, for example

for i = 1,10 do
	if i == 5 then
		continue
	end
	print(i)
end

It will skip through an iteration, basically will print 1-10 but not 5.

3 Likes

i’ve always wondered

why is it continue and not skip?

everytime i see continue i just understand it as “oh yeah keep going with the code”, it’s really confusing to me :sob:

3 Likes

Thanks you guys! I get it now, that’s why return is used in modulescripts, it’s basically a way of giving a value to a function. I also understood continue fairly well, i appreciate yall!

3 Likes

return doesnot give a value to a function, it returns a value from the function
the thing that gives a value to a function is called parameter

function addnumbers(number1, number2)

number1 and number2 are parameters they give a value to the function

1 Like

Most languages use continue; it’s better to just stay consistent

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.