-->
Page 1 of 3

How to correctly code multiple nested IF / THEN

PostPosted: Wed Nov 30, 2016 11:57 pm
by heckler
Hey Group,

I am trying to implement a nested IF/THEN series of tests

Code: Select allif RelayON = -1 then
   if T2Hon = hh then
      if T2Mon = mm then
         RelayON = T2D
         goto [RlyOn]
      end if
   end if
end if


But I am getting an "End If without IF" error

What is the correct way to implement this multi step IF / THEN series??

thanks
dwight

Re: How to correctly code multiple nested IF / THEN

PostPosted: Thu Dec 01, 2016 1:02 pm
by Electroguard
Check your var contents.

Comparison of numbers requires only 1 equals (=) sign, whereas comparison of string characters requires 2 (==)

So your nested IFs must be expecting numeric=VARs else they would error if fed with strings.

I've predefined your vars as numeric to prove that the nested 'ifs' are indeed working correctly... if the inner IF T2Mon = mm THEN it branches, but if it doesn't equate (ie: T2Mon = mm -1) then it drops through without branching.

So I suspect you may have an error in your VAR types, possibly a time "string" instead of the numeric VAL(of the string).


Code: Select allRelayON = -1
hh = 77
mm = 88
T2Hon = hh
T2Mon = mm 'change this to T2Mon = mm - 1
if RelayON = -1 then
   if T2Hon = hh then
      if T2Mon = mm then
         RelayON = T2D
         goto [RlyOn]
      end if
   end if
end if
html "dropped through<br>"
end

[RlyOn]
html "branched<br>"
end

Re: How to correctly code multiple nested IF / THEN

PostPosted: Thu Dec 01, 2016 1:03 pm
by bugs
I have guessed your variable types - think you should decide if RelayON is a string or a number and then let BASIC know before there are any comparisons done.

This code works by specifying string (using ESP Basic 3.0.Alpha 57):-

Code: Select allRelayON = "-1"
T2Hon = "hh"
T2Mon = "mm"

if RelayON = "-1" then        ' if relay off then check if time to turn on for time 2
   if T2Hon = "hh" then
      if T2Mon = "mm" then
         RelayON = "T2D"
         goto [RlyOn]
      end if
   end if
end if

[RlyOn]
end


and this by number:-

Code: Select allT2D = 999
RelayON = -1
T2Hon = "hh"
T2Mon = "mm"

if RelayON = -1 then        ' if relay off then check if time to turn on for time 2
   if T2Hon = "hh" then
      if T2Mon = "mm" then
         RelayON = T2D
         goto [RlyOn]
      end if
   end if
end if

[RlyOn]
end

Re: How to correctly code multiple nested IF / THEN

PostPosted: Thu Dec 01, 2016 1:04 pm
by bugs
Hah! beat me by a minute...