26

I'm just starting out using Lua, and I was wondering (because I can't find it on the website) if Lua has a OR operator, like how in other languages there is ||:

if (condition == true || othercondition == false) {
 somecode.somefunction();
}

whereas in Lua, there is

if condition then
    x = 0
end

how would i write an IF block in Lua to use OR?

Polyov
  • 2,281
  • 2
  • 26
  • 36

1 Answers1

37

With "or".

if condition or not othercondition then
    x = 0
end

As the Lua manual clearly states.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Lua does not have an operator `!`; it uses `~` instead. – Nicol Bolas Jun 30 '12 at 05:02
  • 4
    Not `~` (which is used only in `~=`, i.e. "not equals"), but `not`. Fixed the example in answer. – Alexander Gladysh Jun 30 '12 at 05:37
  • 2
    Voops. Coding in one language, answering in another, inevitably leads to mistakes. – Puppy Jun 30 '12 at 07:04
  • 14
    It should be noted that in Lua, an "or" expression does not necessarily return a boolean value. It returns the first argument (without even executing the second one) if its value is not false or nil. – steve-o Feb 25 '15 at 14:53