Shop OBEX P1 Docs P2 Docs Learn Events
if statments — Parallax Forums

if statments

Can anyone tell me if there are any limitations to how many if statements you can have or nested elseif's? I'm programing in C. thank you

Comments

  • Heater.Heater. Posts: 21,230
    No limit that I have ever found.

    However if your code is nested many levels deep and is wondering off to the right then it's a sign that you should rethink what you are doing. Your function is probably becoming too big and complex to reason about easily or test easily. If there is a problem with it it's probably too big to post here and ask people to look at.

    Clearly you are having a problem in some code you are writing. What?
  • If you nest if statements too deep, you program may become difficult to follow.
  • there is a switch statement in C greatly reducing the pain of nested if else elseif endif constructs.

    Usually I chose to use switch after 2-3 nested if's.

    Spin has a fairly amount of nested if's possible, but there is a final number, I remember something like 32.

    C, as far as I know has no limits there, maybe your compiler might have some.

    Enjoy!

    Mike
  • In Spin, yes there is a limit ... I forget what it is off hand, but I ran into it when I programmed the USB servo controller. I think 16, but not sure.
  • I hit the same limit in spin my work around was continuing on another block of IF's . I'll have to look back at my program.
  • bnikkel wrote: »
    Can anyone tell me if there are any limitations to how many if statements you can have or nested elseif's? I'm programing in C. thank you

    Normally the limit is the address space available to the compiler for holding its parse tree, ie gigabytes on a typical computer these days. You will find the compile-time will become unusable first (unless you switch off optimizations).

    For a particular code generator there may be limits due to the binary object format itself.

    In general a good compiler has no limits other than resource limits, limits are a hardware thing really, not software.
  • my code receives an instruction from a touch screen and then runs through a long list of if statements until it finds one that matches and then jumps to a subroutine and executes some code and jumps back.
  • In Spin I could only get six IF statements on one Tree. So I just continued on a new Tree of Six. Haven't tried it in "C".
  • I changed out quite a few if's and replaced them with switch statements and that seams to be keeping my code on track, I cant find much info on the switch statement though, anyone have any links or advise about it? Mike?
  • cavelambcavelamb Posts: 720
    edited 2018-09-06 17:21
    From the manual: page 60

    case X+Y ' test expression

    Using OTHER
    The optional OTHER component of CASE is similar to the optional ELSE component of an IF structure.

    from my code
    OBJ
      ir      : "IRC"
    '------------------------------
    'other stuff
    '------------------------------
    PUB main
    ' Top of IR Code input loop:
      if IRcog > 0                                    ' if COG loaded okay?
          repeat
            If IRC_ret <> ir#NoNewCode     ' we have a key code
               IRcode := IRC_ret
               ir.Start(IRCpin, @IRC_ret)        ' set up for next code
    
               case IRcode                            ' Parse the key code                            
                 ' control keys
                   ir#power :                 ' <--- Note the colon! 
                                                    '  also note notation = this lable is a constant from 
                                                    ' an  external object (irc.spin file in this case)
                      LCD.CLS  
                      lcd.str(string("Power "))
    
                      if V_Pwr == 1 
                         V_Pwr := 0
                         lcd.str(string("OFF   "))
                         lcd.backlight(0) 
                                                      
                      else
                         V_Pwr := 1
                         lcd.str(string("ON    "))
                         lcd.backlight(1)
                       
                      'indentation shows end of IF V_Pwr
                  ' numeric keys
    
               ' indentation shows end of CASE structure
            'indentation shows end of IF IRC_code
          'indentation shows end of REPEAT
      'indentation shows end of IF IRcog 
    

    The indentation thing is the only thing I really don't care for in Spin.
    Outside the editor it's so NON-obvious about what's going on, and
    simply editing a line of code can radically alter the program structure.

    But that's what we have, so enjoy it...
  • yetiyeti Posts: 818
    edited 2018-09-08 16:57
    OpenSpin is a reimplementation of the original Spin compiler and so should have the same limits: https://github.com/parallaxinc/OpenSpin/blob/master/PropellerCompiler/PropellerCompiler.h#L42-L57

Sign In or Register to comment.