Break

I’ve seen something in code called “Break”, what does this do? And an example of when it’s used would be nice. Thanks ahead of time,

its used to finish a loop, read this for more informations

Ah thankyou.

We should get continue in GMod Lua.

Adding more retarded alternative syntax would give everyone even more bad habits, that’s not good for them if they get to use Lua for something else than GMod later.

You can do this instead:

[lua]for _,v in pairs(ents.GetAll()) do
do
if not v:IsNPC() then
break
end

	print(v)
end

end[/lua]

[lua]Lua 5.1.4 Copyright © 1994-2008 Lua.org, PUC-Rio
> a = {9,8,7,6,5,4,3,2,1}
> for _, num in pairs(a) do
>> do
>> if (num == 3) then
>> break
>> end
>> print(num)
>> end
>> end
9
8
7
6
5
4
>[/lua]
Even if it worked, that’s far stupider than ‘continue’

I fail to see how using continue is a bad habit, whereas doing _Kilburns workaround is good habit:confused:

He was pointing out that it would teach people the magic of continue (even though it’s a widely used operator in a lot of programming languages), but it wouldn’t work in other games/applications using Lua, hence a “bad habit”

Not a good habit, but adding stuff like “continue” to GMod Lua would make some people think it’s a feature of Lua, while it isn’t. Much like &&, ||, //. Pisses me off to no end when I see someone use that.

[editline]10:33PM[/editline]

Oh well, I was pretty much sure it worked.
Just use “repeat … until true” instead of “do … end”, that works. Or find another workaround. Garry disfigured Lua enough like that, I don’t think it needs more syntax additions.

It’s not Lua, it’s GLua:buddy: Can we have it now?

Go ask Garry.

[editline]05:26AM[/editline]

But this one will actually be handy.

Continue is not a bad habit… Continue would add the ability to be more optimized by executing less code in a loop.

Continue stops any code after it from executing while still going through the rest of the array.

Break just exits the whole loop.

It would be usefull.

[editline]01:03AM[/editline]

Would this not let any code execute after it?



for k,v in pairs(ents.GetAll()) do
	do
		if not v:IsNPC() then
			k=k+1     //would it still print or would it move to the next index?
		end
		
		print(v)
	end
end


Even if that does work it would only work for array who’s index uses sequential numbers.

Only one that works is
[lua] for k,v in pairs(ents.GetAll()) do
if v:IsNPC() then
print(v)
end
end[/lua]

[lua] for k,v in pairs(ents.GetAll()) do
if v:IsNPC() then
print(v)
end
end[/lua]

[lua] for k,v in pairs(ents.GetAll()) do
if not v:IsNPC() then
continue
end
print(v)
end[/lua]

I don’t see how one is more optimized than the other. “continue” might make the code a little bit more convenient to write, but other than that, I don’t really see the interest.

If you’ve got a really big loop, it’s neater. Just as it’s neater in a function to go
[lua]function plugh()
if (ply.foo) then
print(“You cannot foo!”);
return
end
bar()
if (ply.baz) then
print(“You cannot baz!”)
return;
end
qux()
if (ply.quux) then
print(“You cannot quux!”)
return;
end
garply()
end[/lua]
than
[lua]function plugh()
if (ply.foo) then
print(“You cannot foo!”);
else
bar()
if (ply.baz) then
print(“You cannot baz!”)
else
qux()
if (ply.quux) then
print(“You cannot quux!”)
else
garply()
end
end
end
end[/lua]

I often use returns (if only performing one check) instead of making a lot of nested if checks, it’s just that I can’t do this in a loop, so it would be easier to use continue.