PDA

View Full Version : if/else if statement




Alhazred
Jul 14, 2011, 10:33 AM
I need to control several values in my app and I'm trying to do so in this way

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.



robbieduncan
Jul 14, 2011, 10:54 AM
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.

dejo
Jul 14, 2011, 12:41 PM
If condition 1 is always true this would explain the behaviour you are seeing.
Especially if condition 1 is something like:
if (a = b)
Yes, I purposely meant to only have one equals sign in there.

robbieduncan
Jul 15, 2011, 04:17 AM
Especially if condition 1 is something like:
if (a = b)
Yes, I purposely meant to only have one equals sign in there.

That is exactly what I was expecting to see if the OP posted their real code.

Alhazred
Jul 20, 2011, 02:41 AM
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.