Village
Village

Reputation: 24373

How to check if a value is greater than one number and less than another number in Lua?

I need to check if a number is greater than or equal to 100 and less than 200.

if 100 =< x < 200 then

What is the proper syntax in Lua?

Upvotes: 4

Views: 13868

Answers (1)

orlp
orlp

Reputation: 117661

This is the correct syntax:

if x >= 100 and x < 200 then
    -- your code
end

Just remember that most programming languages (with Python being a notable exception) don't support chained comparison operators and you have to explicitly compare twice and combine the results of the comparisons with a logical and.

Also, you wrote =< for "greater than or equal to". In almost all programming languages the "greater/smaller than or equal to" operator comes with the comparison character first, e.g.: <=, >=.

Upvotes: 14

Related Questions