Multiple Condition Statement Trouble Newb Question

I don’t understand why the following function works because I’ve set thisAlso = no


this = yes

thisAlso = no

function test()

    if this == yes and

        thisAlso == yes then

        print (“THIS SHOULDN’T PRINT”)

    end

end

test()


Also I don’t understand why and when the == sign is need.  Thanks

That’s because yes and no aren’t types (like numbers, boolean, strings, etc).  In fact I’m sure if you print(this) and print(thisAlso) they will be nil.  This happens because Lua sees yes and no as being references to other variables.  Variables that don’t exist and thus are nil.  

This will work:

this = "yes" thisAlso = "no" function test() if this == "yes" and thisAlso == "yes" then print ("THIS SHOULDN'T PRINT") end end test()

When you say:

something = somethingelse

The thing on the right side of the equals can be only a few specific things:

  • A number (not in quotes)
  • A string (which has to be enclosed in quotes)
  • Another variable (a string not in quotes)
  • boolean:   true or false (not in quotes)
  • nil (meaning empty… not in quotes)
  • a table enclosed in curly braces {}.  Curly braces with nothing in between is an empty table.
  • A function (this is getting beyond the scope of this post)

when you say:

this = yes

It makes the variable this contain the value held in the variable yes.  The variable yes has not bee defined, so it defaults to nil.  So in effect you just said

this = nil

If your intent was for this to hold the string “yes”, you need quotes:

this = “yes”

Now inside your function, you are using an If statement and doing a comparison and asking:

is this equal to (==) yes?   This condition is true since yes is nil and you assigned nil to the variable this.

is thisAlso equal to no?   This conditions is also true, since thisAlso holds the same value as the variable no, which is undefined of nil.  So the total test resolves to:

if true and true then

which is a true condition and your statement prints.

Now if you rewrote this:

this = "yes" thisAlso = "no"   function test()     if this == "yes" and         thisAlso == "yes" then         print ("THIS SHOULDN'T PRINT")     end end  

Thank you both for the detailed responses, I should have paid better attension durring Jay’s Lua Boot Camp Tutorial.

That’s because yes and no aren’t types (like numbers, boolean, strings, etc).  In fact I’m sure if you print(this) and print(thisAlso) they will be nil.  This happens because Lua sees yes and no as being references to other variables.  Variables that don’t exist and thus are nil.  

This will work:

this = "yes" thisAlso = "no" function test() if this == "yes" and thisAlso == "yes" then print ("THIS SHOULDN'T PRINT") end end test()

When you say:

something = somethingelse

The thing on the right side of the equals can be only a few specific things:

  • A number (not in quotes)
  • A string (which has to be enclosed in quotes)
  • Another variable (a string not in quotes)
  • boolean:   true or false (not in quotes)
  • nil (meaning empty… not in quotes)
  • a table enclosed in curly braces {}.  Curly braces with nothing in between is an empty table.
  • A function (this is getting beyond the scope of this post)

when you say:

this = yes

It makes the variable this contain the value held in the variable yes.  The variable yes has not bee defined, so it defaults to nil.  So in effect you just said

this = nil

If your intent was for this to hold the string “yes”, you need quotes:

this = “yes”

Now inside your function, you are using an If statement and doing a comparison and asking:

is this equal to (==) yes?   This condition is true since yes is nil and you assigned nil to the variable this.

is thisAlso equal to no?   This conditions is also true, since thisAlso holds the same value as the variable no, which is undefined of nil.  So the total test resolves to:

if true and true then

which is a true condition and your statement prints.

Now if you rewrote this:

this = "yes" thisAlso = "no"   function test()     if this == "yes" and         thisAlso == "yes" then         print ("THIS SHOULDN'T PRINT")     end end  

Thank you both for the detailed responses, I should have paid better attension durring Jay’s Lua Boot Camp Tutorial.