# ?: Operator

Page 1 of 1

## 5 Replies - 910 Views - Last Post: 26 February 2009 - 11:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=88925&amp;s=016f7ed8d6c5d3eef787dc8c36ac696c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 noorcu

• New D.I.C Head

Reputation: 0
• Posts: 17
• Joined: 11-February 09

# ?: Operator

Posted 25 February 2009 - 01:39 AM

hi i read this statement in a book and i don't know its meaning or what is the alternative for it
(p = p==1 ? 2:1;)
and (p) is intilaized as aglobal variable
Is This A Good Question/Topic? 0

### #3 horace

• D.I.C Lover

Reputation: 654
• Posts: 3,386
• Joined: 25-October 06

## Re: ?: Operator

Posted 25 February 2009 - 02:48 AM

noorcu, on 25 Feb, 2009 - 07:39 AM, said:

hi i read this statement in a book and i don't know its meaning or what is the alternative for it
(p = p==1 ? 2:1;)
and (p) is intilaized as aglobal variable

The conditional operator ? is a ternary operator (it has three operands) in which the operands are three expressions. It takes the following general form:

expression1 ? expression2 : expression3

First expression1 is evaluated and if it is:

true (non zero) expression2 is evaluated and that is the value of the whole expression
false (zero) expression3 is evaluated and that is the value of the whole expression

therefor
```p = p==1 ? 2:1;

```

is shorthand for
```if ( p==1) p= 2 else p=1;

```

Was This Post Helpful? 1

### #4 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: ?: Operator

Posted 25 February 2009 - 03:41 AM

Gotta love that ternary. Great explanation horace.

It also helps when declaring variables where you would want to say something like:
```int x;
if(y>3)
x = y;
else
x = z;

```
instead you could say
```
int x = (y>3)?y:x;

```

or with multiple statements
```int x = (y>3)?y:(z>2)?z:22;
```

Yet one has to wonder when readability is concerned if it is practical to use the operator even if it does save you from using an if or a switch statement.
In the end it all comes down to who will see the code and who will have to understand it.

This post has been edited by carltech: 25 February 2009 - 04:02 AM

Was This Post Helpful? 0

### #5 Hyper

• Banned

Reputation: 108
• Posts: 2,129
• Joined: 15-October 08

## Re: ?: Operator

Posted 26 February 2009 - 10:27 AM

To simplify what the above posters said:
The ? : are an alternative to if/else statements.

This post has been edited by Hyper: 26 February 2009 - 10:28 AM

Was This Post Helpful? 0

### #6 horace

• D.I.C Lover

Reputation: 654
• Posts: 3,386
• Joined: 25-October 06

## Re: ?: Operator

Posted 26 February 2009 - 11:22 AM

Hyper, on 26 Feb, 2009 - 04:27 PM, said:

To simplify what the above posters said:
The ? : are an alternative to if/else statements.

yes - the ? operator is a shorthand alternative to if() then ; else ;
However, like all shorthand use it with care, it can make code unreadable and it is easy to make mistakes.
For example, I have seen numerous cases of programmers thinking they are using the += operator, e.g.
``` x += i;	   // shorthand for x = x + i

```

but instead type
``` x =+ i;

```

Was This Post Helpful? 0

### #7 gabehabe

• GabehabeSwamp

Reputation: 1431
• Posts: 11,001
• Joined: 06-February 08

## Re: ?: Operator

Posted 26 February 2009 - 11:45 AM

The simplest example is a "return the largest of the two" function.

Using an if statement, it would look like this:
```float max(float a, float b) {
if(a > b) {
return a;
} else {
return b;
}
}
```

However, using ternary:
```float max(float a, float b) {return a > b ? a : b;}
```

Let's break it up:
return :: returns the value ~ simple stuff
a > b ? :: is a greater than b?
a : :: return a
b; :: otherwise, return b.

If you wanted to cram an if statement into one line, it would be much longer:
```float max(float a, float b) {if(a > b) return a; else return b;}
```

And finally, I'll put the two right next to each other so you can see the major differences:
```float max(float a, float b) {if(a > b) return a; else return b;}
float max(float a, float b) {return a > b ? a : b;}
```

Hope this helps
Was This Post Helpful? 1

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}