?: Operator

using iostream only

Page 1 of 1

5 Replies - 555 Views - Last Post: 26 February 2009 - 11:45 AM Rate Topic: -----

#1 noorcu  Icon User is offline

  • New D.I.C Head

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

?: Operator

Post icon  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 :rolleyes:
Is This A Good Question/Topic? 0
  • +

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: ?: Operator

Posted 25 February 2009 - 02:48 AM

View Postnoorcu, 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 :rolleyes:

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  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • 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  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: ?: Operator

Posted 26 February 2009 - 11:22 AM

View PostHyper, 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  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • 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