If nesting limit

Kecked

Member
I have a set of if then statements that exceed 20 24 to be exact. I am using them to select when sunrise starts and sunset starts. I know I could use case statements as well but can anyone point me to what is meant by a nested IF. IS this a limit to just 20 if statements in a program or how do they have to be separated not to be considered nested. I looked in the commands but didn't see this limit mentioned.
 

g6ejd

Senior Member
NESTED
Code:
IF a=b THEN
 IF c=d THEN
   IF e=f THEN
     IF g=h THEN
     ENDIF
   ENDIF
 ENDIF
ENDIF
NOT NESTED

IF time= 00 THEN do_something ENDIF
IF time= 01 THEN do_something ENDIF
IF time= 02 THEN do_something ENDIF
IF time= 03 THEN do_something ENDIF
 

nick12ab

Senior Member
The limit is almost certainly the Picaxe stack - I think it is 8 deep :rolleyes:
Not necessarily. It's more likely to be translated into GOTO on <condition> rather than GOSUB on <condition>. And this idea is enforced by this working in the simulator:
Code:
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
if b0 = 0 then
do : loop
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
 

Kecked

Member
nick has it the way I have it. That's why I was wondering why it is called nested when its all in a row like nick shows. That doesn't seem nested to me. Will I have the same issue with case statement? BTW I only used a single endif. Its 24 if thens followed by a single endif.

I'm using two pins to trigger a second micro (I needed more pwms for the leds) so when 10 its sunrise when 01 its sunset. when 00 or 11 don't care. Here is the section it bitches about as nested. I'll redo it as case statements

Code:
'January	
			if month=1 and w22=8  then high b.1:low b.2
			if month=1 and w22=18 then low  b.1:high b.2
	'February	
			if month=2 and w22=8  then high b.1:low b.2
			if month=2 and w22=18 then low  b.1:high b.2
	'March	
			if month=3 and w22=8  then high b.1:low  b.2
			if month=3 and w22=18 then low  b.1:high b.2
	'April	
			if month=4 and w22=8  then high b.1:low  b.2
			if month=4 and w22=18 then low  b.1:high b.2
	'May	
			if month=5 and w22=8  then high b.1:low  b.2
			if month=5 and w22=18 then low  b.1:high b.2
	'June	
			if month=6 and w22=8  then high b.1:low  b.2
			if month=6 and w22=18 then low  b.1:high b.2
	'July	
			if month=7 and w22=8  then high b.1:low  b.2
			if month=7 and w22=18 then low  b.1:high b.2
	'August	
			if month=8 and w22=8  then high b.1:low  b.2
			if month=8 and w22=18 then low  b.1:high b.2
	'September	
			if month=9 and w22=8  then high b.1:low  b.2 
			if month=9 and w22=18 then low  b.1:high b.2
	'October	
			if month=10 and w22=8  then high b.1:low  b.2
			if month=10 and w22=18 then low  b.1:high b.2
	'November	
			if month=11 and w22=8  then high b.1:low  b.2
			if month=11 and w22=18 then low  b.1:high b.201
	'December	
			if month=12 and w22=8  then high b.1:low  b.2
			if month=12 and w22=18 then low  b.1:high b.2
		endif
 

inglewoodpete

Senior Member
Kecked, The code you posted in #5 is nested 1 deep. Well within the capability of the PICAXE and the PE.

g6ejd's post in #3 is correct. Note that nested "Ifs" do not take up stack space, so the only resource used is program memory.

Stack space is only consumed by GoSubs and interrupts; and only then when a GoSub calls a GoSub calls a GoSub etc (nested GoSubs).
 

inglewoodpete

Senior Member
As discussed a couple of years ago there is a nexting limit for IF...THEN statements of 20.

This still applies with the V5.5.1 PE.
I've not tested the limits before. I get a maximum of 19 but it reports a strange error: "Error: can't write to object file."
 

westaust55

Moderator
Yes there is a problem with the Programming Editor V5.5.1 when right on the limit.

Less than 20 nested IF...THEN's and all is well.

at 20 nested IF...THEN's there is the error as IWP states:
"Error: can't write to object file |"​


at 21 nested IF...THEN's there is the expected error message:
"Error: If nesting limit (20) exceeded​
 

Attachments

Goeytex

Senior Member
The Problem with the code in Post 5 is not so much that the nesting limit has been exceeded but that the syntax is wrong. The compiler just caught a nesting limit first. Remove November & December to comply with the nesting limit and you get the syntax " If without Endif".

Certainly these "if" lines should not be "nested" anyway since the first if in the nest is " if month=1 and w22=8 ", which means none of the other lines could ever be true.

Just add an endif at the end of each line like this:

if month=1 and w22=8 then high b.1:low b.2 : endif


But ... I am not sure what you are trying to do with the code, since the same action is performed regardless of month. So it seems that the code needs to be refined.
 
Last edited:

Kecked

Member
I had the endif on each month to start and it still did it. As west said there is a 20 if then limit. No matter I have an idea for a much shorter code using a lookup table to do this. Is there a fact that list other such limits so I can learn them before I hit them?
 

Kecked

Member
Here I think is a more eliquent solution. All I am doing is setting a set of pins high and low to trigger separate chips to do the sunrise and sunset. I ran out of pwms so that was my solution. My final solution will probaly use on of the 16 channel pwm chips. That is why I used two ports so I can address that chip later. I'm also going to dump this approach in favor of a real calculation once I learn this FPU chip.

Code:
'Sunrise

	

Let b27=bcdtobin month					
Lookup b27, (8,7,7,7,6,6,6,7,7,8,8,8),	b28                'sunrise
Lookup b27, (17,18,20, 20,20,21,21,21,21,20,19,18,17), b29 'sunset

	
		
			if hour=b28 then high b.2 :low  b.1:endif 'sunrise
			if hour=b29 then  low b.2 :high b.1:endif 'sunset
 
Last edited:
Top