473,808 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can ternary statements be nested?



Nov 16 '05
14 9190
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*****@micros oft.com> wrote in message
news:Ok******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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*****@micros oft.com> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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*****@micros oft.com> wrote in message
news:Ok******** ******@tk2msftn gp13.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.WriteLi ne("If: {0}", tsIf.TotalMilli seconds);
Console.WriteLi ne("Ternary: {0}", tsTern.TotalMil liseconds);

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*****@micros oft.com> schrieb im Newsbeitrag
news:OM******** ******@TK2MSFTN GP10.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******** ******@tk2msftn gp13.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*****@micros oft.com> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
> 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
4970
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 is called for! Incredible, but true. union mixed
2
3518
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 is called for! Incredible, but true. union mixed
6
2768
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 way assert.h can be implemented without using it? Are these decisions related in anyway?
6
2990
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. I can understand that complicated code would be much less readable using nested operators, but aside from that is there another reason? -- something going on under the hood perhaps which make it inadvisable to use?
48
2768
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 suggestions?
39
6863
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 works fine but it appears that I may have hit a wall with 'if' statements. Due to the number of checks perfromed by the script on the text files, (over 500), there are quite a few 'if' statements in the script, (over 1150). It seems that it...
5
3570
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 you something first before i go to my delimma. codes as follows using Mrs. If Else: #!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>);
0
1295
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 subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which if clause a following else clause would belong: if test1: if test2: print x
21
1197
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
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.