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

How do I test for multiple things in one statement?

Started by diamondpumpkin, 15 March 2016 - 09:24 PM
diamondpumpkin #1
Posted 15 March 2016 - 10:24 PM
I'm trying to test for multiple different numbers in the same line, but I can't quite figure it out…

Code:

rednet.open("top")
local id, message = rednet.receive("test")
if message == "1", "2", "3", or  "4" then
   -- code

It throws an error if I use this (then expected). How would I do the correct if statement?
Dragon53535 #2
Posted 15 March 2016 - 10:39 PM
The keywords 'and' and 'or'.

PIL page


if message == "1" or message == "2" or message == "3" or message == "4" then

Remember though that and takes precedence over or.

message2 is just a variable i'm using in place of some random other variable just to show

if message == "1" or message2 == "2" and message == "2" then
--#Is interpreted just as this:
if message == "1" or (message2 == "2" and message == "2") then
Edited on 15 March 2016 - 09:41 PM
KingofGamesYami #3
Posted 15 March 2016 - 11:26 PM
When you have a large number of such comparisons, a table can be useful.


local t = {['1']=true,['2']=true,['3']=true,['4']=true}
if t[ var_to_check ] then

end