luafreak
luafreak

Reputation: 115

Lua: If a number is between 1 and 20

I wanted to check if a number is between 1 and 20, this is what I am using:

for x=1,20 do
  if x == 10 then
    print(x)
  end
end

The problem is that, it prints the number 10 instead of printing true or 1 2 3 .. am I doing something wrong here? If so, what’s it? Thanks.

Upvotes: 2

Views: 19925

Answers (2)

Ian
Ian

Reputation: 442

In your example, you're telling it to print x when x is 10, so it can only print 10. It's doing exactly as you asked of it.

But what you really want is:

if x >= 1 and x <= 20
   -- Do stuff
end

Upvotes: 1

lud
lud

Reputation: 2001

Do you want to check mutiple numbers, or just one like this :

my_number = 10
if my_number >= 1 and my_number <= 20 then
    print 'it is!'
end

Upvotes: 11

Related Questions