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

How ComputerCraft Intelligence works?

Started by Hayden_Almeida, 16 May 2015 - 01:12 AM
Hayden_Almeida #1
Posted 16 May 2015 - 03:12 AM
Hello guys, i have one doubt, lets say i have this code:


	  if rs.getInput("front") and rs.getInput("back") then
				monitor.setCursorPos(11,3)
		elseif rs.getInput("front") then
				monitor.setCursorPos(11,3)
		elseif rs.getInput("back") then
				monitor.setCursorPos(11,3)
		else
				monitor.setCursorPos(9,3)
		end

I want to know what is the difference when i use various if instead of elseif ?
Like this:

	  if rs.getInput("front") and rs.getInput("back") then
				monitor.setCursorPos(11,3)
		end
		if rs.getInput("front") then
				monitor.setCursorPos(11,3)
		end
		if rs.getInput("back") then
				monitor.setCursorPos(11,3)
		else
				monitor.setCursorPos(9,3)
		end

How ComputerCraft will "READ" in both of cases?
Edited on 16 May 2015 - 01:13 AM
valithor #2
Posted 16 May 2015 - 03:27 AM
The main difference is using elseifs only 1 of the pieces of code will run, while using if's technically all of them can run.

For example in this specific case if there is redstone in both front and back all 3 will run if there is only if's, while only the first one would run when using elseif's. This is to say when using elseif's it will check each one of the conditions in the order they are put, and it will only run the first one whose conditions are true.

More in depth description:

Sort explanation of if statements.

if condition then --# checks if condition is true and if it is true, it runs the code between this and the elseif
elseif condition2 then--# will only run if the previous one did not and condition2 is true
elseif condition3 then --# will only run if both of the previous ones did not run and condition3 is true
end

if condition then --# will run if condition is true
end
if condition2 then --# will run if condition2 is true, previous one does not matter
end
if condition3 then --# will run if condition3 is true, previous two do not matter
end
Edited on 16 May 2015 - 01:40 AM
KingofGamesYami #3
Posted 16 May 2015 - 03:42 AM
In the case of your first example, if there is input on the front and back of the computer, none of the elseif statements are executed.

In the case of your second, each input will be checked individually.

Comments:

Example #1:

if rs.getInput("front") and rs.getInput("back") then --#if this is true then
  monitor.setCursorPos(11,3)                         --#none of the elseifs are checked
elseif rs.getInput("front") then --#if this is true then
  monitor.setCursorPos(11,3)     --#none of the elseifs below this one are checked
elseif rs.getInput("back") then --#etc.
  monitor.setCursorPos(11,3)
else --#if all previous ifs / elseifs were false
  monitor.setCursorPos(9,3) --#the else statement executes
end

Example #2:

if rs.getInput("front") and rs.getInput("back") then --#checks this
  monitor.setCursorPos(11,3)
end
if rs.getInput("front") then --#checks this, regardless of whether or not first if statement was true.
  monitor.setCursorPos(11,3)
end
if rs.getInput("back") then --#same as above
  monitor.setCursorPos(11,3)
else --#if not rs.getInput( "back" )
  monitor.setCursorPos(9,3)
end

This link may be worth a read: http://www.lua.org/pil/4.3.1.html
Edited on 16 May 2015 - 01:43 AM
Hayden_Almeida #4
Posted 16 May 2015 - 04:33 AM
In the case of your first example, if there is input on the front and back of the computer, none of the elseif statements are executed.

In the case of your second, each input will be checked individually.

Comments:

Example #1:

if rs.getInput("front") and rs.getInput("back") then --#if this is true then
  monitor.setCursorPos(11,3)						 --#none of the elseifs are checked
elseif rs.getInput("front") then --#if this is true then
  monitor.setCursorPos(11,3)	 --#none of the elseifs below this one are checked
elseif rs.getInput("back") then --#etc.
  monitor.setCursorPos(11,3)
else --#if all previous ifs / elseifs were false
  monitor.setCursorPos(9,3) --#the else statement executes
end

Example #2:

if rs.getInput("front") and rs.getInput("back") then --#checks this
  monitor.setCursorPos(11,3)
end
if rs.getInput("front") then --#checks this, regardless of whether or not first if statement was true.
  monitor.setCursorPos(11,3)
end
if rs.getInput("back") then --#same as above
  monitor.setCursorPos(11,3)
else --#if not rs.getInput( "back" )
  monitor.setCursorPos(9,3)
end

This link may be worth a read: http://www.lua.org/pil/4.3.1.html

Thanks. i understand now :D/>