Why are my if statements not working correctly?

Apr 15, 2010 at 1:56 PM

I have an if statement to determine if my first variable has an entry in it. If it does have an entry then it will send the textbox input to my second variable. The problem is that Addon Studio wont let me properly close the if statement. It will show the vertical line on the left hand side of where the statements are being closed out. For instance without the if statement the vertical line will extend from the function blah() to the very bottom end which is what it is suppose to do. This isnt the exact code for the if statement because I dont have the file infront of me but I dont think the code inside the if statement would matter. When I put the if statement in without the end to close out the if the addon will not work at all. I am assuming it is because the if statement was not closed out. When I attempt to close out the if statement like the following code the vertical line that shows the different functions being closed out the vertical line will extend from the function blah() to the end statement for the if which is the second to last if statement which isnt correct. The addon, like I assumed, did not work even though I had closed out the if statement appropriately. Am I doing something wrong and does anyone have any references for how to implement if statements. I have done quite a bit of research and have not been able to find very helpful resources.

function blah()

   if a == "" then

     var1 = textbox input

   elseif

     a != "" then

     var2 = textbox input

   end

end

Apr 15, 2010 at 11:23 PM

 

The main problem is the use of the c/c++ !=(Not Equal), this is not used in LUA and instead the ~= is used.  I personally use the NOT command but its personal preference. Both examples ive provided below so you can compare.  Its also a good idea to not put the relational operators on a different line of the IF statement like in your example - although it may work its less readable when debugging.

Using NOT:

function blah()
  if(a == "") then
     var1 = textbox input;          -- this line is run if a variable is blank
  elseif(not(a=="")) then
     var2 = textbox input;          -- this line is run when variable a is not blank
  end;
end;

Using the ~= (Not Equal To)

function blah()
  if(a == "") then
     var1 = textbox input;          -- this line is run if a variable is blank
  elseif(a~="") then
     var2 = textbox input;          -- this line is run when variable a is not blank
  end;
end;

For your reference, a IF/While etc can use the following relational operators:

==   Equal To
~=   No Equal
<     Lower Than
>     Greater Than
<=   Lower Than or Equal
>=   Greater Than or Equal

 

Apr 15, 2010 at 11:25 PM

 

As an extension to my last post, a good place if your unsure of the various commands available to you is to use the LUA manual as a reference:

http://www.lua.org/manual/5.1/