Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-14-2002, 05:37 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
c++ ternary operator...

this category is lagging .. so i thought i'd post something cool i learned in c++

the ternary operator ( ? : )

(condition 1) ? (expression 1) : (expression 2 )

so .. if condition 1 is true, then use expression 1 .. else use expression 2

whatFruitToBuy = ( i like apples ) ? buy apples : buy grapes;

anyhow .. just saves some if/else statements.
sde is offline   Reply With Quote
Old 06-15-2002, 12:47 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
The constraints how ever, are that the first condition must return in a true or false value.. But since 0 is false and any other value is true, theres not that much constraining in it.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-15-2002, 05:11 AM   #3 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
but

if(10=2){hi;}{bye;}


is so much neater..

oh well.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 06-15-2002, 08:56 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
thanks redhead that is a good point ..

so you are saying that it is just the equivelent to:

if(condition 1)
{
expression1;
} else {
expression1;
}

and that is not saving you if/else statements, .. it's just saving you 1 if else statement.

... i guess it's useful as shorthand .. also. you can use it even in your cout statement or defining a variable.. i do like that.

i.e. variable = (condition1) ? expression1 : expression2;

or

cout << "the ternary result is " << ((condition1)?expression1:expression2);

i was trying to get it to work in the cout statement for a while the other night, but then i realized you must have the surrounding " ( ) " to make it work.

Last edited by sde; 06-15-2002 at 09:03 AM.
sde is offline   Reply With Quote
Old 06-15-2002, 12:22 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
The nice thing about it, is you can use it in something like:
Code:
void* my_function (int is_true)
{
     return (is_true ? 'e' : 10);
}
So if you dont want to do alot of if/else with the returning values, then you can use it as a shortcut..
And the examples you gave are also in that direction, nice and simple..

And the requirement for the sorrounding () in a cout statement is, because '<<' has higher precedence than the '?' operator.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 09:48 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 11:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting