473,385 Members | 1,757 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,385 software developers and data experts.

can ternary statements be nested?



Nov 16 '05 #1
14 9157
yes
Nov 16 '05 #2
coooool,, thanks
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
yes

Nov 16 '05 #3
Hi there,

Any sample to do nested ternary. Interesting!

Thanks.
--
Regards,
Chua Wen Ching :)
"Josh" wrote:
coooool,, thanks
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
yes


Nov 16 '05 #4
> Any sample to do nested ternary. Interesting!

int a = 1;

int b = 2;

int c = 3;

int d = 4;

int e = 5;

int f = 5;

int g = 5;

int r;

r = (a<b) ? (c<d) ? e : f : g;

It returns 5
Nov 16 '05 #5
You can nest them, but it's not usually good for readability.

Console.WriteLine((i < 0) ? "negative" : ((i > 0) ? "positive" :
"zero"));

P.
Nov 16 '05 #6
> You can nest them, but it's not usually good for readability.
True but Im trying to write a single expression to check for collison
between 3d objects its already a nightmare!!!
Nov 16 '05 #7
I wouldn't create such constructs at all - totally unreadable (even this is
only a sample).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Josh" <so*****@microsoft.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Any sample to do nested ternary. Interesting!


int a = 1;

int b = 2;

int c = 3;

int d = 4;

int e = 5;

int f = 5;

int g = 5;

int r;

r = (a<b) ? (c<d) ? e : f : g;

It returns 5

Nov 16 '05 #8
Hi,

It's kind of how you can traverse a matrix using a single for in C :)
I still remember the first time I delivered a homework with that in the
univ. the teacher gave me a 2 ( suspense in Cuba's grades ) just for that :)

It's better using two lines of code, or at least separate them in more than
one line, and use parentesis.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Josh" <so*****@microsoft.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Any sample to do nested ternary. Interesting!


int a = 1;

int b = 2;

int c = 3;

int d = 4;

int e = 5;

int f = 5;

int g = 5;

int r;

r = (a<b) ? (c<d) ? e : f : g;

It returns 5

Nov 16 '05 #9
Hi,

Then make it the most readable possible something like:

r = (a<b) ?
(c<d) ? e : f
: g;

With one condition per line, instead of all in the same line.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Josh" <so*****@microsoft.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
You can nest them, but it's not usually good for readability.

True but Im trying to write a single expression to check for collison
between 3d objects its already a nightmare!!!

Nov 16 '05 #10
Josh:

I guess you've already heard opinions on readability (I agree, nested
ternarys are hard to read). If you are having to do very complex compares
like this, you might try analysing your logic with a Karnaugh map. You
might find a way to simplify the compares. I've never used one myself, but
I keep this link tucked away, just in case:
http://www.ee.surrey.ac.uk/Projects/.../karrules.html

"Josh" <so*****@microsoft.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
You can nest them, but it's not usually good for readability.

True but Im trying to write a single expression to check for collison
between 3d objects its already a nightmare!!!

Nov 16 '05 #11
I would consider breaking the nested structure into seperate structures only
if that would process quicker. Speed is everything what I'm doing. Some
experiments are needed.
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:el**************@tk2msftngp13.phx.gbl...
I wouldn't create such constructs at all - totally unreadable (even this is only a sample).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Josh" <so*****@microsoft.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Any sample to do nested ternary. Interesting!


int a = 1;

int b = 2;

int c = 3;

int d = 4;

int e = 5;

int f = 5;

int g = 5;

int r;

r = (a<b) ? (c<d) ? e : f : g;

It returns 5


Nov 16 '05 #12
Hi all,

This is an amazing post. I just want to ask.

Ternary VS IF statements.. which one improves performance?

For readability, I think if will wins the war. But can you all share your comments.

Thanks.
--
Regards,
Chua Wen Ching :)
"J.Marsch" wrote:
Josh:

I guess you've already heard opinions on readability (I agree, nested
ternarys are hard to read). If you are having to do very complex compares
like this, you might try analysing your logic with a Karnaugh map. You
might find a way to simplify the compares. I've never used one myself, but
I keep this link tucked away, just in case:
http://www.ee.surrey.ac.uk/Projects/.../karrules.html

"Josh" <so*****@microsoft.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
You can nest them, but it's not usually good for readability.

True but Im trying to write a single expression to check for collison
between 3d objects its already a nightmare!!!


Nov 16 '05 #13
On 03 Jul 2004 12:14, "Chua Wen Ching" wrote:
Hi all,

This is an amazing post. I just want to ask.

Ternary VS IF statements.. which one improves performance?

For readability, I think if will wins the war. But can you all share your
comments.

Thanks.


(Note that any attempt to meaningfully benchmark performance needs far
more effort than this.)
I ran the following under Debug mode:

DateTime start = DateTime.Now;
string result;
for (int i = 0; i < 100000000; i++) {
if (A()) {
if (B()) {
result = "AB";
} else {
result = "A-B";
}
} else {
if (B()) {
result = "-AB";
} else {
result = "-A-B";
}
}
}
DateTime endIf = DateTime.Now;
for (int i = 0; i < 100000000; i++) {
result = (A() ? (B() ? "AB" : "A-B") : (B() ? "-AB" : "-A-B"));
}
DateTime endTern = DateTime.Now;
TimeSpan tsIf = endIf - start;
TimeSpan tsTern = endTern - endIf;
Console.WriteLine("If: {0}", tsIf.TotalMilliseconds);
Console.WriteLine("Ternary: {0}", tsTern.TotalMilliseconds);

The output was:
A True, B True: If: 2859.375 Ternary: 2484.375

Difference ~ .3 seconds

A True, B False If: 2796.875 Ternary: 2437.5

Difference ~ .36

A False, B True If: 2765.625 Ternary: 2312.5

Difference ~ .45

A False, B False If: 2578.125 Ternary: 2453.125

Difference ~ .12

In other words, over 100,000,000 repititions there was far less than .5
secs difference. I don't see how this is going to make any difference to
any program you might consider writing with .NET.
It *is* interesting that the least difference (by a long way) is when
they're both false,
Readability is in the eye of the beholder, but I know which one my eye's
on :)
--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #14
The ternary operator will create the same code which would also created by
an if statement. So at the end, they are the same, only that the ternary
operator is much harder to read.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Josh" <so*****@microsoft.com> schrieb im Newsbeitrag
news:OM**************@TK2MSFTNGP10.phx.gbl...
I would consider breaking the nested structure into seperate structures only if that would process quicker. Speed is everything what I'm doing. Some
experiments are needed.
"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:el**************@tk2msftngp13.phx.gbl...
I wouldn't create such constructs at all - totally unreadable (even this

is
only a sample).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Josh" <so*****@microsoft.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
> Any sample to do nested ternary. Interesting!

int a = 1;

int b = 2;

int c = 3;

int d = 4;

int e = 5;

int f = 5;

int g = 5;

int r;

r = (a<b) ? (c<d) ? e : f : g;

It returns 5



Nov 16 '05 #15

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

Similar topics

3
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
2
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
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...
39
by: slogging_away | last post by:
Hi - I'm running Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32, and have a script that makes numerous checks on text files, (configuration files), so discrepancies can be reported. The script...
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: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on...
21
by: K Viltersten | last post by:
In the code at our company i see the following. if (someStuff != null) { if (someStuff != "") { doThatThing (); } } While it's fully correct and valid, i'd like to rewrite it as follows.
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.