-1

Possible Duplicate:
What does the question mark and the colon (?: ternary operator) mean in objective-c?

I have seen code where it uses a syntax something like...

someValue = someBoolean ? valueOne : valueTwo;

Or something like this.

I've never used this and I'm not sure what it's called.

Please can someone explain how to use it or provide a link to a resource about it.

Community
  • 1
  • 1
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Thanks very much! I was surprised to find I actually got the syntax 100% right :D – Fogmeister Jan 08 '13 at 13:51
  • LOL! I think the whole of SO knew about this apart from me :D – Fogmeister Jan 08 '13 at 13:56
  • 1
    I believe this is called a Ternary operator, I am not sure though! ;-) – Geert Jan 08 '13 at 13:57
  • 3
    It seems there are some reputation whores over here, 7 answers that differs only in formatting :p – toasted_flakes Jan 08 '13 at 14:00
  • Just accept the most complete, timing shouldn't matter :) – toasted_flakes Jan 08 '13 at 14:02
  • @grasGendarme: might be you answered few seconds earlier then us, but FAQs says... linked answers are not to be provided. So you can alter your answer and compile it to a better and most efficient :) – Anoop Vaidya Jan 08 '13 at 14:03
  • LOL, I'll just accept the highest voted then :D Didn't mean to cause any disturbance :D – Fogmeister Jan 08 '13 at 14:03
  • 1
    It is a nice thing in some cases. However, do not overdo it! In rather complex structures it can easily make your code quite unreadable. You may not notice that while wrting. But wait a wee or to until you have to debug that very line of code. :) – Hermann Klecker Jan 08 '13 at 14:51

6 Answers6

8

It's ternary opertaor.

It evaluates the someBoolean condition.

If it is true then pass the valueOne to someValue

If it is false then pass valueTwo to someValue

It is equal to:

if(someBoolean)
{
   someValue = valueOne;
}
else
{
   someValue = valueTwo;
}

This is a good link which explains about ternary operator

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
3

This is called ternary operator ( ?: )

1 ? 2 : 3

1 is the condition.

2 is executed when 1 it is true.

3 is executed when 1 is false.

Similar to: (Below is not a running code, 1,2,3 shows only placeholders for some expressions and statements.

 if(1){ //condition
     2 //true
 }
 else{
     3 //false
 }

You can shorten it as for :

int bigger;
(10<100) ? bigger=100 : bigger=10;

in short way:

int bigger = (10<100) ? 100 : 10 ;

NOTE:

Its precedence order is among the least and it is much slower then if-else and switch case statements.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • *it is much slower then if-else and switch case statements* — do you have some reference to back this up? – vikingosegundo Jan 09 '13 at 05:01
  • @vikingosegundo : I read somewhere years ago that ternary is compiled to if-else. and ternary are processed left to right, thus resulting in few stack calls and resulting it slower than if-else. and more more than 5 conditions :? and if-else are slower than switch case. – Anoop Vaidya Jan 09 '13 at 07:03
  • who would chain 5 of them? switch can only be used with integer values so the conditional operator can just be unfolded into if-else. But you stated, switch is slower that if-else. do you have any proof on that? especially with modern compilers? – vikingosegundo Jan 09 '13 at 17:00
2

It is a ternary operator (also known as the conditional operator). You can find explanation at this link.

Basically your expression is saying that if someBoolean is true someValue will get valueOne if not it will get valueTwo.

It is similar to:

 if(someBoolean)
 {
    someValue = valueOne;
 }
 else
 {
    someValue = valueTwo;
 }

which offers less visibility in your code. I recommend using this operator in case you want to assign a value which depends on one condition.

Note that it is an expression not specific to Objective-C, you can use it in C and C++ too.

tiguero
  • 11,477
  • 5
  • 43
  • 61
  • 1
    and also C, where it originates in this form, while it first was introduces in [CPL](http://en.wikipedia.org/wiki/%3F:). +1 for adding the alias name. – vikingosegundo Jan 09 '13 at 05:12
  • You are right i don't know why i didn't mention it at the first place – tiguero Jan 09 '13 at 09:29
0

The result of the assignment is valueOne is the condition is true, and valueTwo if the condition is false.

See it here on wikipedia. It also makes the case with other languages, just skip them and see the C syntax example.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
0

Suppose user needs to answer some question and you change background color of your view to red if he was wrong, green if he was correct.

- (void)handleAnswer:(BOOL)correct {
    UIColor *color = (correct) ? [UIColor greenColor] : [UIColor redColor];
    self.view.backgroundColor = color;
}
dariaa
  • 6,285
  • 4
  • 42
  • 58
0

It works same as the following

if (someBoolean)
{
    someValue = valueOne;
}
else
{
    someValue = valueTwo;
}
Exploring
  • 925
  • 6
  • 18