if/else if statement

Discussion in 'iPhone/iPad Programming' started by Alhazred, Jul 14, 2011.

  1. macrumors member

    Joined:
    Jul 5, 2011
    #1
    I need to control several values in my app and I'm trying to do so in this way
    Code:
    if(condition 1)
    {
       //do something1
    }
    else if(condition 2)
    {
       //do something2
    }
    else if(condition 3)
    {
       //do something3
    }
    else
    {
       //do something else
    }
    
    but it looks that only the 1st condition is checked, the other are ignored.
    Is it possible to use this kind of control in objective-c? If not, how to do something that behaves like that?
    I'm checking if some text fields are filled before to send the data.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Post the real code. What you are suggesting works just like normal C so there must be something specific to your code. Of course if condition 1 is true we'd not expect any of the other conditions to be checked. If condition 1 is always true this would explain the behaviour you are seeing.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Especially if condition 1 is something like:
    Code:
    if (a = b)
    Yes, I purposely meant to only have one equals sign in there.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That is exactly what I was expecting to see if the OP posted their real code.
     
  5. thread starter macrumors member

    Joined:
    Jul 5, 2011
    #5
    Thanks for your answers, the problem was only due to a lack of attention, what I want to do with this code is to check if required fields of a form are filled in or not.
    The problem was that I have a label on the side of each field which shows a text "Field required" if the related field is not filled in, but in the code I had the messages all linked to the 1st label and it seemed to me that the 1st condition was always true.
     

Share This Page