This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Emil's profile picture

My if statement isn't working

Started by Emil, 09 August 2013 - 07:43 AM
Emil #1
Posted 09 August 2013 - 09:43 AM
Title: My if statement isn't working.

I'm trying to make a program that when I type test 10 it says hi and when I use another number it says fail but it always returns fail even if i type test 10.


local arg=...

if arg == 10 then

print("hi")

else

print("fail")

end
Bubba #2
Posted 09 August 2013 - 09:45 AM
Split into new topic.

The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
TheOddByte #3
Posted 09 August 2013 - 10:10 AM
Split into new topic.

The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
Or just make 10 a string


-- Changing 10 to a string
local arg= ...

if arg == "10" then -- The arg is a string and should be compared with a string
  print("hi")

else
  print("fail")
end

--Changing arg to a number
local arg= tonumber(...)

if arg == 10 then
  print("hi")

else
  print("fail")
end
Bubba #4
Posted 09 August 2013 - 10:13 AM
Split into new topic.

The reason for this is that arguments are passed into programs as strings. So in order for that comparison to work, you have to use tonumber(arg) and convert it into an actual number.
Or just make 10 a string


-- Changing 10 to a string
local arg= ...

if arg == "10" then -- The arg is a string and should be compared with a string
  print("hi")

else
  print("fail")
end

--Changing arg to a number
local arg= tonumber(...)

if arg == 10 then
  print("hi")

else
  print("fail")
end

Sure. or use tonumber so you can do useful number operations. The only reason to use a string to compare is if you want to save yourself about five or six characters.