Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 7th, 2008, 10:56 AM   #1
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
What does ? : do?

I am unsure of what the ? : is doing in Java. Ive searched in Google I cannot find anything, I have forgotten which book I read it out of but heres an example of some code that involves it:
61    public void setGrossSales( double sales )
62    {
63       grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64    }
namsu is offline   Reply With Quote
Old Jan 7th, 2008, 11:35 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: What does ? : do?

A question mark is known as a "conditional operator". Essentially, it's a one line if statement.

http://www.google.ca/search?hl=en&q=...operator&meta=

It evaluates the statement before the question mark. If it's true, the statement inherits the value of the left side of the colon. If the statement is false, it inherits the value on the right side of the colon.

statement that will be true or false ? value on left side of colon if true : value on right side of colon if false

Is equal to:

if (statement that will be true or false)
    use value on left side
else
    use value on right side


So the code you posted:

public void setGrossSales( double sales )
{
    grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}

Is equivilent to:

public void setGrossSales( double sales )
{
    if (sales < 0.0)
        grossSales = 0.0;
    else
        grossSales = sales;
}

It's an easy way to prevent the number from being negative, in one line.
Sane is offline   Reply With Quote
Old Jan 7th, 2008, 11:37 AM   #3
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
Re: What does ? : do?

Quote:
Originally Posted by Sane View Post
A question mark is known as a "conditional operator". Essentially, it's a one line if statement.

http://www.google.ca/search?hl=en&q=...operator&meta=

It evaluates the statement before the question mark. If it's true, the statement inherits the value of the left side of the colon. If the statement is false, it inherits the value on the right side of the colon.

statement that will be true or false ? value on left side of colon if true : value on right side of colon if false

Is equal to:

if (statement that will be true or false) is (true)
    use value on left side
else
    use value on right side
Thanks
namsu is offline   Reply With Quote
Old Jan 7th, 2008, 3:01 PM   #4
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: olympia,WA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: What does ? : do?

@namsu you don't have to quote his entire post to say thank you.

I hardly ever see the ? operator. Using if or if else looks better as in more human readable than ?. It's style to me.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jan 7th, 2008, 3:30 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: What does ? : do?

Well there are instances where it becomes necessary in order to prevent repeating code, or adding new variables in to the scenario. I'll make something up to demonstrate this...

Quote:
The function "isPrime(x)" returns 1 if x is prime, 0 if it is composite.

Using this function, output whether or not n is a prime number in the following format:

"The number 'n' is a prime number."
/ or /
"The number 'n' is a composite number."

E.G.

"The number '4' is a composite number."
"The number '6079' is a prime number."
So what would be the best way to code it? With if/else statements:

(Switching to C Code now)

C Syntax (Toggle Plain Text)
  1. if (isPrime(n))
  2. printf("The number '%d' is a prime number.", n);
  3. else
  4. printf("The number '%d' is a composite number.", n);

With a conditional operator:

C Syntax (Toggle Plain Text)
  1. printf("The number '%d' is a %s number.", n,
  2. isPrime(n) ? "prime" : "composite");

Each has their drawbacks, but personally I'd go with the latter, for sake of not repeating code.
It's easier to change the format of the output, and the input specifications.
Sane is offline   Reply With Quote
Old Jan 7th, 2008, 3:30 PM   #6
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
Re: What does ? : do?

Quote:
Originally Posted by mrynit View Post
@namsu you don't have to quote his entire post to say thank you.

I hardly ever see the ? operator. Using if or if else looks better as in more human readable than ?. It's style to me.
Yeah sorry I will just say Thanks and post next time.
namsu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC