473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

anything whcih can be done with ternary but not with if else

HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?
Thanks
Keshav

Oct 17 '05 #1
24 1777
gu**********@gmail.com wrote:
HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?


class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};

Oct 17 '05 #2
Rolf Magnus wrote:
gu**********@gmail.com wrote:
HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?


class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};


And conditionally assigning constants in general. Compare:

void Foo( const bool b )
{
const int i = b ? 1 : 2;
// ...
}

void Bar( const bool b )
{
// i cannot be const
int i = 0;
if( b )
{
i = 1;
}
else
{
i = 2;
}
// ...
}

Cheers! --M

Oct 17 '05 #3
<gu**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com
HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?
Thanks
Keshav


bool b;
int x, y;

// stuff setting b, x and y

int & ref = b ? x, y;

Note that you can do

if(b)
int & ref = x;
else
int & ref = y;

but the scope of the reference is limited to the body of the if-else
conditions. You can't get around this by declaring the reference before the
if statement because a reference must be initialised at its point of
declaration.

--
John Carson

Oct 17 '05 #4
what if I'm doing this?

int const * foo( const bool b )
{
static int i = 0;

if( b )
{
i = 1;
}
else
{
i = 2;
}
return &i;
}

Oct 17 '05 #5
int const * foo( const bool b )
{
static int i;
return &(i=b ? 1 : 2);
}
This is the equivalent using the ternary operator making the function as
short as possible. This doesn't give an example that the OP is looking
for.

Fraser.
Oct 17 '05 #6
shailendra wrote:
what if I'm doing this?

int const * foo( const bool b )
{
static int i = 0;

if( b )
{
i = 1;
}
else
{
i = 2;
}
return &i;
}


It should work fine, and you could use the ternary operator, though not
on the initialization of the static:

int const* foo( const bool b )
{
static int i = 0; // Only executed once
i = b ? 1 : 2; // Executed each time foo is called
return &i;
}

Cheers! --M

Oct 17 '05 #7
My reply was to the examples given by "mlimber" that, there is a way to
substitute his/her idea --

So given example is wrong, coz this isn't the only situatuation where
ternery can be applied. So we better find some good situation only for
ternery.

Frankly speaking, I dont think, there is any!!

Oct 17 '05 #8
On Mon, 17 Oct 2005 05:49:18 -0700, gupta.keshav wrote:
HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?


I'm not sure there is such a case *if* you're allowed to put the if/else
in a separately called function.

e.g.

class Foo
{
public:
Foo(bool b)
: x_(getit(b))
{
}

int getit(bool b)
{
if (b)
return 42;
else
return 7;
}
private:
const int x_;
};

- Jay
Oct 17 '05 #9
shailendra wrote:
My reply was to the examples given by "mlimber" that, there is a way to
substitute his/her idea --

So given example is wrong, coz this isn't the only situatuation where
ternery can be applied. So we better find some good situation only for
ternery.

Frankly speaking, I dont think, there is any!!


One has already been given.

--
Mike Smith
Oct 17 '05 #10
<gu**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Is there any situation which can be handled by ternary operator but
not with if else blocks?


if-else requires assignability. Anything that doesn't support assignment
cannot be used with if-else. In addition to the already provided constructor
initialization list, three more that I can think of:
1) A class that doesn't define operator=:

Class UnAssignable{ /* ... */ };

/* copy construction */
UnAssignable object = condition ? first : second;
2) Initializing a reference:

Class & reference = condition ? first : second;
3) Initializing a constant:

SomeType const object = condition ? first : second;

Ali

Oct 17 '05 #11
You aren't replacing the ternary operator with if-else block but with a
function call with if-else block inside it. There's a subtle
difference, I think. A good suggestion, just happened to think, "wait
just a minute...!"

Oct 17 '05 #12
mlimber wrote:
Rolf Magnus wrote:
gu**********@gmail.com wrote:
Is there any situation which can be handled by ternary operator
but not with if else blocks?


class Foo
{
public:
Foo(bool b) : x_(b ? 42 : 7) { }
private:
const int x_;
};


And conditionally assigning constants in general. Compare:

void Foo( const bool b )
{
const int i = b ? 1 : 2;
// ...
}


In this case it can be done with if...else:
void Foo( const bool b )
{
int i_;
if (b)
i_ = 1;
else
i_ = 2;
const int i = i_;
}

Oct 17 '05 #13
Rolf Magnus wrote:
gu**********@gmail.com wrote:
Is there any situation which can be handled by ternary operator but
not with if else blocks?


class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};


Note that this doesn't imply that ?: adds any new functionality
to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).

However I can't think of a replacment for (b ? foo() : bar()) offhand.

Oct 17 '05 #14
wht can be done is

bool b;
int x,y,z;
if (b) z= x; else z = y;
int &i = z;

Oct 18 '05 #15
What can also be done with ternary is it can be put
into a comma operator list, perhaps as part of a macro
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 18 '05 #16
Greg Comeau wrote:
What can also be done with ternary is it can be put
into a comma operator list, perhaps as part of a macro


And it can be used in template arguments.

Oct 18 '05 #17
<gu**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
wht can be done is

bool b;
int x,y,z;
if (b) z= x; else z = y;
int &i = z;


That is not equivalent to

int &i = b ? x, y;

Suppose that the code is followed by:

i = 6;

With my code, either x or y will become 6. With your code, x and y are not
affected; the change is to z.
--
John Carson

Oct 18 '05 #18
ben
Old Wolf wrote:
Rolf Magnus wrote:
gu**********@gmail.com wrote:

Is there any situation which can be handled by ternary operator but
not with if else blocks?


class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};

Note that this doesn't imply that ?: adds any new functionality
to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).

However I can't think of a replacment for (b ? foo() : bar()) offhand.


int f(bool b)
{
if (b) return foo();
else return bar();
}

class Foo
{
public;
Foo(bool b)
: x_(f(b))

.....

ben
Oct 18 '05 #19
On Mon, 17 Oct 2005 13:26:31 -0700, persenaama wrote:
You aren't replacing the ternary operator with if-else block but with a
function call with if-else block inside it. There's a subtle
difference, I think. A good suggestion, just happened to think, "wait
just a minute...!"


You're quite right. I mostly mentioned it because, believe it or not, I
have seen coding guidelines that forbid the use of the ternary operator!
So knowing how to live without it can be useful (though I wouldn't want to).

- Jay
Oct 18 '05 #20

Ali Çehreli wrote:
<gu**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Is there any situation which can be handled by ternary operator but
not with if else blocks?


if-else requires assignability. Anything that doesn't support assignment
cannot be used with if-else. In addition to the already provided constructor
initialization list, three more that I can think of:
1) A class that doesn't define operator=:

Class UnAssignable{ /* ... */ };

/* copy construction */
UnAssignable object = condition ? first : second;
2) Initializing a reference:

Class & reference = condition ? first : second;
3) Initializing a constant:

SomeType const object = condition ? first : second;

Ali


I don't think overloading of ternary operator is possible in C++

Oct 18 '05 #21
"shailendra" <su********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Ali Çehreli wrote:
<gu**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Is there any situation which can be handled by ternary operator but
not with if else blocks?


if-else requires assignability. Anything that doesn't support assignment
cannot be used with if-else. In addition to the already provided
constructor
initialization list, three more that I can think of:
1) A class that doesn't define operator=:

Class UnAssignable{ /* ... */ };

/* copy construction */
UnAssignable object = condition ? first : second;
2) Initializing a reference:

Class & reference = condition ? first : second;
3) Initializing a constant:

SomeType const object = condition ? first : second;

Ali


I don't think overloading of ternary operator is possible in C++


Did you reply to my message by mistake? The reason I am asking is that I
can't understand what part you may be referring to. (The lack of o period at
the end of your sentence makes me believe that you really sent it by
mistake, at least prematurely.)

Maybe you thought that the pairs of first/second above were the same objects
in all three cases? If so, no, I didn't mean them to be so; think of each
pair as being defined separately in each case.

Even if I did mean to use the same first and second in all three cases, it
would still work, provided a suitable relationship (inheritance, constructor
parameter, existence of type conversion operators, etc.) among UnAssignable,
Class, SomeType, and the type used for first and second.

Ali

Oct 18 '05 #22
John Carson schrieb:
bool b;
int x, y;

// stuff setting b, x and y

int & ref = b ? x, y;


int *p;
if (b)
p = &x;
else
p = &y;

int &ref2 = *p;

Thomas
Oct 18 '05 #23
"Thomas J. Gritzan" <Ph*************@gmx.de> wrote in message
news:dj**********@newsreader3.netcologne.de
John Carson schrieb:
bool b;
int x, y;

// stuff setting b, x and y

int & ref = b ? x, y;


int *p;
if (b)
p = &x;
else
p = &y;

int &ref2 = *p;

Thomas


Yes, a nice workaround (albeit one that comes at the cost of an extra
variable and extra assignment).

--
John Carson

Oct 19 '05 #24
Sorry Ali, yes it was a mistake

Oct 19 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zipher | last post by:
It seems the debate over PEP 308 (if-then-else expression) occurred prior to the arrival of generator expressions. Mightn't this new latter syntax be the ticket to a "one obvious way" to write a...
2
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this With regards,...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.