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

  • 11 Comments sorted by Date Added Votes
  • 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.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • 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
    I am just another Code Monkey.
    A determined coder can write COBOL programs in any language. -- Author unknown.
    Press any key to continue, any other key to quit

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this post are to be interpreted as described in RFC 2119.
  • 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.


    Beau Schwabe -- Submicron Forensic Engineer
    www.Kit-Start.com - bschwabe@Kit-Start.com ෴෴ www.BScircuitDesigns.com - icbeau@bscircuitdesigns.com ෴෴

  • 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: 679
    edited September 6 Vote Up0Vote Down
    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: 439
    edited September 8 Vote Up0Vote Down
    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

    ◁ Stay OmmmmmmPtimistic! ▷ ◁ No Source – No Go! ▷ ◁ Please help: http://rosettacode.org/wiki/Category:Spin ▷ ◁ Why Asimov's Laws of Robotics Don't Work - Computerphile ▷ ◁ DNA is a four letter word. ▷
Sign In or Register to comment.