Any limit to nesting "if" commands?

moxhamj

New Member
Does anyone know if there are any limitations to nesting "if" commands eg
if b0=1 then
if b2=1 then
if b3=1 then
endif
endif
endif

Gosubs can be nested 4 deep and any more than that and the program will give no error message or warning and just start going haywire. I'm wondering if there are any similar limitations to the "if" command?

Thinking about answering my own question and having written compilers in the past I think all if/then/else commands can be broken down to goto statements at compile time eg
if a condition is false then goto (either else or endif)
continue code if it was true
goto endif
else - location for goto from above
continue code
endif
continue code

So no stacks are needed and so nesting should not be a problem. Is this correct?

Also another question - how does the picaxe compiler handle this example of bad programming:
main:gosub mysubroutine
goto main

mysubroutine:
code
goto main
code
return

When mysubroutine is called this will put the return location onto the stack, but if there is a jump out of the subroutine the return won't get called. This is a simple example -the goto could have jumped to another location then a goto to a third location then a goto main. Does the stack then overflow and the program start behaving strangely in the same way that nesting too many gosubs causes strange behaviour?

Help would be most appreciated!
 

westaust55

Moderator
For next loops can be nested to 8 deep,

Gosubs can be nested to 4 deep on most PICAXE models and 8 deep on the X1/X2 parts

If Then's are not specified in the manual.

Lets just say it appears to be greater than 20 on all models.
Seems like one for Technical to verify
 
Last edited:

moxhamj

New Member
Thanks Hippy. I'm up to 8 so should be fine. The nested gosub limitation is probably the biggest problem but there are workaround including using a goto at the second last nested loop instead of a gosub, then the return at the end of that sub returns back to Main. If only there was more program space in an 18X...
 

westaust55

Moderator
so, is this a case of the following example of nested if...then statements using a single variable, albeit a trivial example, not being classed as a real test: :confused:

Code:
if b0 > 0 then . . .
  if b0 > 1 then . . .
  if b0 > 2 then . . .
   if b0 > 3 then . . .
    if b0 > 4 then . . .
     "here on large number
    endif
  endif
 enif
endif
 

Technical

Technical Support
Staff member
There are occasions when the same variable could be nested, but most meaningful nesting would use different variables.

The example quoted is indeed meaningless, it is the same as
if b0 > 4
'here on large number
end if
 

westaust55

Moderator
nested IF. . .THEN staments

Dr Acula,

Does anyone know if there are any limitations to nesting "if" commands eg
if b0=1 then
if b2=1 then
if b3=1 then
endif
endif
endif
Not sure exactly what your program is trying to achieve, depends whether the variables (in your example) could all be true at the same time, but if not, another construct/format to try might be:

Code:
IF b0 = 1 THEN
  Statement 1
ELSEIF b2 = 1 THEN
 Statement 2
ELSEIF b3 = 1 THEN
  Statement 3
ENDIF
 

Dippy

Moderator
I think Drac was just putting a simple example in fairness.
There are sometimes when a degree of nesting is required, esp if there are several conditions that need ANDing.
But , as you suggest, sensible coding and structuing means that you should never have to go very deep.
 
Top