ScriptUO
Official ScriptUO EasyUO Scripts => Script Debug => Topic started by: Grandewd on October 14, 2013, 08:26:12 PM
-
Clear this up for me, once and for all pls...
If "if" is satisfied by a single condition are brackets required - (that may be the wrong syntax, so let me show you what I'm trying to say........
Can I do this:
If #findcnt = 0
do this one thing <<<< this doesn't have to be bracketed, right?
else
do this thing <<<<<and do I need a bracket for these 3 here?
do this thing
do etc....
Or do I have to do this:
If #findcnt = 0
{
do this one thing
}
else
{
do this thing
do this thing
do etc....
}
-
Brackets are required for multiple statements... However they're not required when there will be only one statement to execute. I would personally prefer to utilize them just to keep code clean, neat and effectively helps when debugging code... I believe that excess lines though will hurt the overall runtime of EUO scripts as it executes literally line by line, so that's the draw back (If you're a stickler over fractions of milliseconds being wasted).
Example:
if #findcount = 0
{
do this one thing
}
is the same as
if #findcount = 0
do this one thing
Incorrect use would be:
if #findcount = 0
do this thing
also do this thing
and this thing
This is incorrect because everything after "do this thing" (also do this thing, and this thing) will always execute regardless of your if statement. That is where brackets are required.
This carries over into all execution regions that would exceed a single line.
Example:
In this example, either "do this" OR "do this 2" will execute while "do this 3" and "do this 4" will always execute, because EUO doesn't understand that it's an entire region specific to only the if-else statement.
if #findcount = 0
do this
else
do this 2
do this 3 ;will execute regardless of if-else statement
do this 4 ;will execute regardless of if-else statement
So you would need brackets containing the else statement's execution region.
if #findcount = 0
do this
else
{
do this 2
do this 3
do this 4
}
Vice versa:
if #findcount = 0
{
do this
do this also
}
else
do this 2
And ofcourse:
if #findcount = 0
{
do this
do this also
}
else
{
do this 2
do this 3
do this 4
}
-
Excellent...
So brackets are NOT needed, either after if or after else - unless there is more than a single statement to execute!
I agree, that brackets help debug (especially if I haven't seen the script in a while) - but as long as I understand what is and isn't executed, there's no reason why I can't skip some annoying brackets...
I tend to write my scripts in bits and pieces and tie it together as I go along... I test everything before I move on....
-
I always use {} myself - then it is easy to add stuff as I develop.
-
I actually put both the brackets to define a given regions as the firs thing I do simply so that I don't forget that end closing bracket heh.
-
It became a force of habit for me in Stealth. When I write an if-then or if-then-else statement I add the begin and ends (which are basically the same thing as brackets) before I add the execution statements.
IE:
if( ) then
begin
end;
if( ) then
begin
end
else begin
end;
while( Connected ) AND ( NOT Dead ) do
begin
if( ) then
begin
end;
end;
Then I fill in everything else, it basically allows me to start with a nice clean template.