The ternary if-else

Need explanation on syntax

Page 1 of 1

5 Replies - 2916 Views - Last Post: 30 January 2007 - 08:03 AM Rate Topic: -----

#1 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

The ternary if-else

Posted 30 January 2007 - 06:51 AM

a = --b ? b : (b = -99); 
--b ? b : (b = -99);


I am reading a book on c++ so this "Ternary Operator if-else" is explained with these 2 sample statements like examples. Problem is I cannot understand them. Anyone post some examples with explanation will be appreciated.

Evgeni
Is This A Good Question/Topic? 0
  • +

Replies To: The ternary if-else

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: The ternary if-else

Posted 30 January 2007 - 06:56 AM

http://mathbits.com/...g/operator2.htm

(a>b) ? (c=25) : (c=45);


is the same as
if (a>b)
{
	   c=25;
 }
 else
{
	   c=45;
 }


Was This Post Helpful? 0
  • +
  • -

#3 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: The ternary if-else

Posted 30 January 2007 - 06:58 AM

http://en.wikipedia.org/wiki/%3F:
Was This Post Helpful? 0
  • +
  • -

#4 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: The ternary if-else

Posted 30 January 2007 - 07:12 AM

Great help guys, thank you. 8 min later. Really fast respone KUDOS! (next time ill wikepedia it ,though:D)
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: The ternary if-else

Posted 30 January 2007 - 07:34 AM

View PostZammy, on 30 Jan, 2007 - 01:51 PM, said:

a = --b ? b : (b = -99); 
--b ? b : (b = -99);


I am reading a book on c++ so this "Ternary Operator if-else" is explained with these 2 sample statements like examples. Problem is I cannot understand them. Anyone post some examples with explanation will be appreciated.

Evgeni

this is one of most convoluted examples of the ? ternary operator I have seen as it also combines -- and assignment in the same expression

  a = --b ? b : (b = -99)


is equivalent to
  if(--b)
		 a = b;
  else
		 a = b = -99;


(a) if b is 1, --b gives 0 (false) hence b is assigned -99 and this is the value of the expression
(b) if b is not 1, --b gives b = b-1 (true) and this is the value of the expression

the result of the expression is then assigned to a
Was This Post Helpful? 0
  • +
  • -

#6 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: The ternary if-else

Posted 30 January 2007 - 08:03 AM

Very stupid example to give then. No idea why that guy tried to make it so complicated. Thank you also.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1