If Statement Confusion

Hi, I’m repliicated and I am confused on something about if statements. Sometimes, when I look at code examples, I see this:

if condition then
  print("something")
end

To make it clear, it just says something, like condition in this case. I don’t understand why, so could someone make it clear? Thanks.

1 Like

Is it something like if the condition is true then do something?

1 Like

Yes, if condition is not nil or false then it will print “something”

1 Like

The condition, in any way possible has to evaluate to true for the if-statement to run. Meaning you can either just leave it as condition or condition == true. For checking if it’s false or nil, you can simply do not condition or condition == false. In both of these, the former is more concise so you’ll see more uses of that.

4 Likes

From the Lua 5.1 docs:

Boolean is the type of the values false and true . Both nil and false make a condition false; any other value makes it true.

So any value can be used as a boolean/condition. In situations where something acts as e.g. a condition in an if statement or while loop, the value is treated as true or false as described above.

1 Like