• Break
    15 replies, posted
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 [URL="http://www.lua.org/pil/4.4.html"]this[/URL] for more informations
Ah thankyou.
[QUOTE=Dave_Parker;22481681]We should get continue in GMod Lua.[/QUOTE] 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]
[QUOTE=_Kilburn;22482254]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][/QUOTE] [lua]Lua 5.1.4 Copyright (C) 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 [u]bad[/u] habit, whereas doing _Kilburns workaround is [u]good[/u] habit:confused:
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] [QUOTE=Lexic;22483531]Even if it worked, that's far stupider than 'continue'[/QUOTE] 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?
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? [code] 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 [/code] 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]
Sorry, you need to Log In to post a reply to this thread.