473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how does ?: work

Tom
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom
Nov 16 '05 #1
7 1829
Yep, it's a conditional.

test ? true_outcome : false_outcome;

So, say you want to add one if the value is less than 5, zero
otherwise:

value = value + (value < 5) ? 1 : 0;

Peace,
--Carl

Nov 16 '05 #2
Tom wrote:
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom

string path = pathvar.trim() == string.empty ? defaultpath : pathvar;

if pathvar.trim returns string.empty then path will be set to
defaultpath, otherwise pathvar.

HTH
JB
Nov 16 '05 #3
Watch for word wrap on the link:

http://msdn.microsoft.com/library/de...aloperator.asp
Nov 16 '05 #4
hi
The conditional operator (?:) returns one of two values depending on the
value of a Boolean expression.

syntax is
cond-expr ? expr1 : expr2
cond-expr is An expression of type bool.
expr1 An expression.
expr2 An expression.
Remarks
If cond-expr is true, expr1 is evaluated and becomes the result;
if cond-expr is false, expr2 is evaluated and becomes the result.
Only one of expr1 and expr2 is ever evaluated.
regards
Ansil
Trivandrum
"Tom" wrote:
Hi

Is this a conditional ? what is the structure of the statement?
ch Tom

Nov 16 '05 #5
"Tom" <To**********@h otmail.com> wrote in news:ON******** ******@TK2MSFTN GP11.phx.gbl:
Hi

Is this a conditional ? what is the structure of the statement?


Tom,

http://msdn.microsoft.com/library/de...alOperator.asp

or

http://tinyurl.com/46weh

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #6
Hello
The + operator takes precedence over the ?: operator.
It shoulr be written
value = value + (value < 5? 1 : 0);

Best regards,
Sherif
<ca***********@ gmail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
Yep, it's a conditional.

test ? true_outcome : false_outcome;

So, say you want to add one if the value is less than 5, zero
otherwise:

value = value + (value < 5) ? 1 : 0;

Peace,
--Carl

Nov 16 '05 #7
"Tom" <To**********@h otmail.com> wrote in message
news:ON******** ******@TK2MSFTN GP11.phx.gbl...
Hi

Is this a conditional ? what is the structure of the statement?


Lots of good answers already, but I'll add my 2 cents: ?: is nothing but
shorthand for a longer construction. A tediously common pattern in almost
any programming context is to have to write something of the form:

if(condition)
variable = value1;
else
variable = value2;

Kernighan and Ritchie, when they cooked up C many years ago, recognized that
the point of that code, its underlying semantics, is to assign to the
variable. But the assignment has become disguised by wrapping it in an if
statement. Recognizing that this is such a common pattern they cooked up
the ?: operator to allow developers to make the intention of their code more
explicit. Thus, instead of hiding the assignment inside of an if(), you
hide the if() inside of the assignment:

variable = condition ? value1 : value2;

Not only can this make your code (slightly) more self-documenting, if the
actual variable name in question is something long and complicated, the ?:
version can certainly be easier to type. Either way--written as an if()
block or as an assignment--both pieces of code should compile to exactly the
same machine code (or IL or whatever).

The drawback, of course, is that this solution is not very scalable to
situations where you have more than a boolean choice to make. People who
favor the use of ?: can be tempted to nest them, as in:

variable = condition1?valu e1:(condition2? value2:value3);

but this sort of thing quickly--no, immediately--becomes difficult to read,
understand, debug, and maintain. Personally, I don't use ?: very much
myself, and generally only in the context of parameters to function calls;
if I have a lengthy function call, something like:

function(paramt er1, parameter2, paramater3,
"some long string",
parameter4, parameter5);

and then I find that in some circumstances parameter5 needs to have one or
the other value, then it's much easier to write:

function(paramt er1, parameter2, paramater3,
"some long string",
parameter4,
condition?value 1:value2);

than it is to replicate the entire function call inside an if(condition)
block. It's also easier to maintain if I end up changing the values of some
of the other parameters. At any rate, when used wisely ?: can make your
code shorter, easier to read, and easier to maintain. Used unwisely, it can
turn otherwise clean code into an obfuscated mess.
Nov 16 '05 #8

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

Similar topics

7
4845
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does not exist... What can I do about this and way does it not work in win 98?
3
3736
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why the update does not work? <% Set ConnGallery = Server.CreateObject("ADODB.Connection") ConnGallery.Open ConnectionString Set cmdTemp = Server.CreateObject("ADODB.Command")
5
3622
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug puposes only and is not normally visable to the user.) The Thread function is actually in the Form class. Now.. What I am seeing is that when I create an instance of this Class Library's Form, which starts the worker thread, it seems to hose up...
22
2606
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under Linux Suse 7.1. I can't belive that C++ exists. Thanks in advance ! robert.bralic@si.htnet.hr
12
2937
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now sometimes Win 2000 Clients can't connect to the database. They connect over TCP/IP. The db2log says that the function getHostByName failed. It seems to be only on new installed win 2000 Workstations. Already installed WS have NOT this problem.
0
2363
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed". (hidden textboxes ect.) 2. How do get a control to get focus when the page is loaded (think username textfield on a loginpage) 3. Does the tab-order work just like in regular forms (1 ->2 -> 3 and so on?) 4. How does tabindex work with datagrid...
14
4840
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
89
6035
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
14
3471
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src="" id="eventImage0" class="eventImage"><img src="" id="eventImage1" class="eventImage"></div>
1
7095
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
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...
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.